Re-declaring a variable will not destroy the value of that variable, until and unless it is assigned with some other new value. If we look at the following example variables "x" and "y" were assigned with values 4 and 8 respectively, later on when those variables were reassigned, the old values were replaced with the new values and displayed as shown in the output. Example: Re-declaring with New Values var x = new Number(4); ... Read More
In JavaScript, prototypes allow you to add properties and methods to constructor functions. You can modify prototypes even after objects are created, and all existing instances will automatically inherit the changes. Understanding Prototypes Every function in JavaScript has a prototype property. When you create objects using a constructor function, they inherit methods and properties from the constructor's prototype. Example: Modifying Prototypes Modifying Prototypes body { ... Read More
When working with arrays containing numbers, null, and undefined values, we need to compute sums while treating these falsy values as zero. This is a common scenario when processing data from APIs or user inputs. Let's say we have an array of arrays, each containing some numbers along with some undefined and null values. We need to create a new array that contains the sum of each corresponding sub-array elements, treating undefined and null as 0. Following is the sample array: const arr = [[ 12, 56, undefined, 5 ], [ ... Read More
In this tutorial, we will learn how to print object arrays in JavaScript. An array of objects is a collection that stores multiple objects with similar structure in a single variable. Each object can contain multiple properties, making it useful for storing complex data like user information, product details, or any structured data. Let's explore different methods to print arrays of objects in JavaScript. Using JSON.stringify() Method The JSON.stringify() method converts JavaScript objects into JSON strings, making it perfect for displaying arrays of objects in a readable format. Syntax JSON.stringify(value, replacer, space) ... Read More
In this tutorial, we will explore how to replace a particular substring in a string using regular expressions in JavaScript. Sometimes we may want to replace a recurring substring in a string with something else. In such cases, regular expressions can be beneficial. Regular expressions are basically a pattern of characters that can be used to search different occurrences of that pattern in a string. Regular expressions make it possible to search for a particular pattern in a stream of characters and replace it easily. For example, consider the regular expression /ab*c/ which denotes that we are looking for ... Read More
The document.images property in JavaScript provides access to all elements in a document. It returns an HTMLCollection containing all image elements, allowing you to count, access, and manipulate images programmatically. Syntax document.images document.images.length document.images[index] document.images.namedItem(name) Properties and Methods The document.images collection provides: length - Returns the number of images in the document [index] - Access image by numeric index (0-based) namedItem() - Access image by name or id attribute Example: Counting Images ... Read More
To set the left position of a positioned element in JavaScript, use the style.left property. This property works on elements that have a CSS position value of absolute, relative, or fixed. Syntax element.style.left = "value"; The value can be specified in pixels (px), percentages (%), or other CSS units like em, rem, etc. Example Here's how to change the left position of a button when clicked: #myButton { ... Read More
HTML5 Canvas composition refers to how new shapes are drawn in relation to existing content on the canvas. The globalCompositeOperation property controls how newly drawn shapes combine with existing pixels. What is globalCompositeOperation? The globalCompositeOperation property determines the blending mode for new shapes. It affects transparency, layering, and how colors mix when shapes overlap. Syntax context.globalCompositeOperation = "operation-type"; Common Composition Types There are 12 main composition operations that control how new shapes interact with existing canvas content: ... Read More
IndexedDB is a powerful client-side database API that allows you to store structured data in the browser. Here's how to add data to an IndexedDB database. Setting Up IndexedDB Before adding data, you need to open a database and create an object store: let db; // Open IndexedDB database const request = indexedDB.open("EmployeeDB", 1); request.onupgradeneeded = function(event) { db = event.target.result; const objectStore = db.createObjectStore("employee", { keyPath: "id" }); console.log("Database setup complete"); }; request.onsuccess = function(event) { ... Read More
In this article, we are going to discuss a Doubly Linked List Class data structure in JavaScript. This is a linear data structure. Doubly linked lists are almost the same as a singly linked list in all operations, we just need to keep track of one extra link per node. In singly linked lists, we just had next links, in doubly linked lists, we have 2 links, next and prev. Structure of Doubly Linked List Doubly linked lists are represented as: ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance