The includes() method checks whether an array contains a specific element, while splice() is used to add or remove items from an array. Together, they can be used to remove multiple elements from an array efficiently. Syntax array.includes(searchElement) array.splice(start, deleteCount) How It Works The approach involves iterating through the array and using includes() to check if each element should be removed. When a match is found, splice() removes it, and the index is decremented to account for the array shift. Example deleteElementsFromArray = function(elements, ...values) { let elementRemoved ... Read More
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 More
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 More
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 More
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 More
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 More
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 More
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 More
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 More
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 More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance