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
Javascript Articles
Page 404 of 534
Explain in detail about Mark and Sweep algorithm in JavaScript?
Mark and Sweep Algorithm The Mark and Sweep algorithm is JavaScript's primary garbage collection method that identifies objects "which are unreachable" rather than objects "which are no longer needed". This algorithm is an improvement over the Reference-counting algorithm and solves the circular reference problem. How Mark and Sweep Works The algorithm operates in three important steps: Root Identification: The algorithm starts from "roots" - global variables accessible in the code. In browsers, the window object acts as the primary root. Marking Phase: The algorithm traverses from roots to find ...
Read MoreHow can Detached DOM elements cause memory leak in JavaScript?
Detached DOM elements are nodes that have been removed from the DOM tree but still exist in memory because JavaScript variables maintain references to them. This prevents the garbage collector from freeing the memory, leading to memory leaks. Understanding DOM Tree Structure The DOM is a double-linked tree structure where each node holds references to its parent and children. When you maintain a JavaScript reference to any node, the entire subtree remains in memory even after removal from the visible DOM. DOM Tree ...
Read MoreHow can Forgotten timers or callbacks cause memory leaks in JavaScript?
JavaScript memory leaks commonly occur when timers and callbacks maintain references to objects that should be garbage collected. Understanding these patterns helps prevent memory issues in web applications. How Timers Create Memory Leaks When objects are referenced inside timer callbacks, the garbage collector cannot release them until the timer completes. If timers run indefinitely or reset themselves, the referenced objects remain in memory permanently. Timer Functions Overview JavaScript provides two main timing functions: setTimeout() - Executes a function once after a specified delay setInterval() - Executes a function repeatedly at specified intervals ...
Read MoreHow to implement quick sort in JavaScript?
In this article, we are going to discuss how to implement quick sort in JavaScript with suitable examples. Quick Sort The Quick sort is a divide and conquer algorithm similar to the merge sort. In this, we pick a pivot element and divide the array around the pivot element. There are many ways to pick the pivot element. Always pick the first element as a pivot element. Always pick the last element as a pivot element. (Implemented in the below program) Pick a random element as pivot ...
Read MoreHow can closures cause memory leak and how to prevent it?
Closures are one of JavaScript's powerful features, allowing inner functions to access variables from their outer (parent) functions. However, this can sometimes lead to memory leaks when variables remain in memory longer than necessary. Understanding Closure Memory Leaks A memory leak occurs when variables from outer functions remain accessible to inner functions, preventing garbage collection even when those variables aren't actively used. The JavaScript engine keeps these variables in memory because the inner function might reference them. Example of Potential Memory Leak function parentFunction(arg1) { ...
Read MoreHow to freeze an Object in JavaScript?
In JavaScript, Object.freeze() makes an object immutable by preventing modifications to its properties. Once frozen, you cannot add, delete, or modify properties of the object. Syntax Object.freeze(obj) Parameters obj: The object to freeze. Returns the same object that was passed to the function. How Object.freeze() Works When an object is frozen: New properties cannot be added Existing properties cannot be removed Existing property values cannot be changed The object's prototype cannot be changed Example: Basic Object Freezing // Create an object with properties ...
Read MoreWrite a palindrome program in JavaScript so that only alphanumeric values should be allowed?
A palindrome is a string that reads the same forwards and backwards. When checking palindromes with alphanumeric characters only, we need to ignore spaces, punctuation, and special characters, considering only letters (a-z) and digits (0-9). For example, "A man, a plan, a canal: Panama" becomes "amanaplanacanalpanama" after removing non-alphanumeric characters, which is indeed a palindrome. Basic Palindrome Check Here's a simple approach using string manipulation methods to check if a string is a palindrome: var str = "racecar"; ...
Read MoreHow to Title Case a sentence in JavaScript?
Title case formatting converts the first letter of each word in a sentence to uppercase while keeping the remaining letters lowercase. This is commonly used for headings, titles, and proper formatting of text. Algorithm Split the sentence into individual words using string.split() method Convert all letters in each word to lowercase using string.toLowerCase() method Loop through each word and capitalize the first letter using toUpperCase() Concatenate the capitalized first letter with the remaining lowercase letters Join all words ...
Read MoreHow to Split large string in to n-size chunks in JavaScript?
A string is an order of one or more characters that contain numbers, letters, symbols, or special characters. In JavaScript strings are immutable. i.e., once you create a string you cannot change its value. For example, consider the following snippet here we have created a string variable and assigned a value (Tutorialspoint) to it. In the next statement, we are trying to change the contents of the string at the first index. Then we are displaying the contents of the string. let x = 'Tutorialspoint'; x[0] = 't'; console.log(x); // Tutorialspoint ...
Read MoreHow to prevent modification of object in JavaScript ?.
JavaScript provides three methods to prevent modification of objects at different levels of restriction. These protective measures ensure that objects cannot be accidentally or intentionally altered, maintaining code integrity and predictable behavior. Three Levels of Object Protection 1) Prevent Extensions Object.preventExtensions() prevents new properties from being added to an object, but existing properties can still be modified or deleted. Example var object1 = { prop1: 1 }; Object.preventExtensions(object1); delete ...
Read More