Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
Rotating an array - JavaScript
In JavaScript, rotating an array means moving elements from one position to another by a specified number of steps. This operation is useful for circular data structures and algorithmic problems. For example, if we have an array [12, 6, 43, 5, 7, 2, 5] and rotate it by 3 positions to the left, the result would be [5, 7, 2, 5, 12, 6, 43]. Method 1: Using Array Prototype Extension We can extend the Array prototype to add a rotation method: // Helper function to rotate by one position const rotateByOne = arr => { ...
Read MoreFinding the length of a JavaScript object
JavaScript objects don't have a built-in length property like arrays. However, there are several methods to find the number of properties in an object. Suppose we have an object like this: const obj = { name: "Ramesh", age: 34, occupation: "HR Manager", address: "Tilak Nagar, New Delhi", experience: 13 }; We need to count the number of properties in this object. Using Object.keys() (Recommended) The most common and reliable method is Object.keys(), which returns an array of the object's property names: ...
Read MoreHow to get the difference between two arrays in JavaScript?
To get the difference between two arrays in JavaScript, you can find elements that exist in one array but not in both. This is commonly called the symmetric difference. There are several approaches to achieve this. Using filter() and includes() (Recommended) Array Difference function arrayDifference(arr1, arr2) { // Elements in ...
Read MoreWhat are the three types of errors I can expect in my JavaScript script?
In this tutorial, let us discuss the three types of errors we can expect in our JavaScript code. Errors are statements that block the execution of the program. During the compilation and execution of JavaScript programs, three types of errors can occur: syntax errors, runtime errors, and logical errors. Syntax Errors Syntax errors are the most common errors that occur when the JavaScript engine cannot understand the code due to incorrect syntax. These errors happen during the parsing phase before code execution begins. For example, using a semicolon instead of a colon in an object declaration ...
Read MoreHow to define a measurement in screen pixels with CSS?
To define a measurement in screen pixels with CSS, use the px unit. The pixel (px) is an absolute unit that represents a single dot on the screen and provides precise control over element sizing and positioning. Syntax property: value px; Example: Using Pixels for Positioning and Sizing .pixel-example { position: relative; left: 90px; ...
Read MoreHow to check whether a number is finite or not in JavaScript?
In JavaScript, the isFinite() method checks whether a value is a finite number. It returns true for finite numbers and false for infinite values, NaN, or non-numeric values. Syntax isFinite(value) Parameters: value - The value to be tested for finiteness Return Value: Returns true if the value is finite, false otherwise. Example 1: Basic Usage Here's a function that demonstrates basic usage of isFinite(): Check Finite Numbers Checking division results: ...
Read MoreHow to remove a class name from an element with JavaScript?
To remove a class name from an element with JavaScript, you can use the classList.remove() method. This method provides a clean and efficient way to manipulate CSS classes dynamically. Syntax element.classList.remove('className'); Example .newStyle { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; width: 100%; padding: 25px; background-color: rgb(147, 80, 255); color: white; ...
Read MoreFilter the properties of an object based on an array and get the filtered object JavaScript
In JavaScript, you often need to filter an object's properties based on specific criteria. This article shows how to create a filtered object containing only the properties whose keys appear in a given array. Problem Statement We need to write a function that takes an object and an array of string literals, then returns a new object containing only the key-value pairs where the key exists in the array. For example: If the object is {"a": [], "b": [], "c": [], "d": []} and the array is ["a", "d"], the output should be: {"a": [], ...
Read MoreSorting an array of objects by property values - JavaScript
In JavaScript, you can sort an array of objects by property values using the Array.sort() method with a custom comparison function. This is particularly useful when working with data structures like product catalogs, user lists, or any collection of objects. Sample Data Let's work with this array of home objects: const homes = [ { "h_id": "3", "city": "Dallas", "state": "TX", ...
Read MoreWhat are runtime errors in JavaScript?
There are three types of errors in programming: (a) Syntax Errors, (b) Runtime Errors, and (c) Logical Errors. Runtime errors, also called exceptions, occur during execution (after compilation/interpretation). For example, the following line causes a runtime error because here the syntax is correct, but at runtime, it is trying to call a method that does not exist. window.printme(); // printme() method doesn't exist Uncaught TypeError: window.printme is not a function Common Types of Runtime Errors JavaScript throws different types of runtime errors depending on the issue: ...
Read More