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
Programming Articles
Page 887 of 2547
What is the main difference between objects created using object literal and constructor function?
The main difference between objects created using object literal and constructor function lies in how they handle references and instances. Objects created with object literals are referenced by variables, while constructor functions create independent instances. Let's explore both approaches with examples to understand this fundamental difference. Objects Created Using Object Literal When you create an object using object literal syntax and assign it to multiple variables, all variables point to the same object in memory. This means any change made through one variable affects all other variables referencing that object. Example ...
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 MoreExplain in detail about Reference-counting garbage collection in JavaScript?
Reference-counting garbage collection is the simplest garbage collection algorithm used in JavaScript. This algorithm monitors objects and removes those that have no references pointing to them. An object becomes eligible for garbage collection when its reference count drops to zero. How Reference-Counting Works The algorithm maintains a count of how many references point to each object. When a reference is created, the count increases. When a reference is removed, the count decreases. Objects with zero references are immediately marked for garbage collection. Example: Basic Reference Counting var obj = { x: ...
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 find the minimum value of an array in JavaScript?
JavaScript provides several methods to find the minimum value in an array. The most efficient approach is using Math.min() with the spread operator, though other methods like sorting are also available. Using Math.min() with Spread Operator (Recommended) The Math.min() function finds the smallest number among its arguments. Combined with the spread operator (...), it can easily find the minimum value in an array. Find Minimum function findMinimum() { var numbers = [6, 39, 55, 1, 44]; var minimum = Math.min(...numbers); ...
Read MoreWhat is non-enumerable property in JavaScript and how can it be created?
Objects can have properties that don't show up when iterated through the particular object using Object.keys() or for...in loop. Those type of properties are called as non-enumerable properties. What are Non-enumerable Properties? Non-enumerable properties are object properties that are hidden from enumeration methods like Object.keys(), for...in loops, and Object.entries(). However, they can still be accessed directly using their property name. Creating Non-enumerable Properties To create a non-enumerable property we have to use Object.defineProperty() method. This is a special method to create non-enumerable properties in an object. Syntax Object.defineProperty(object, propertyName, { ...
Read MoreWhat is global namespace pollution in JavaScript?
Global namespace pollution occurs when too many variables and functions are declared in the global scope, leading to name collisions. This is especially problematic in large projects using multiple JavaScript libraries. What is Name Collision? Name collision happens when two or more scripts define variables or functions with the same name in the global scope. The last definition overwrites previous ones, causing unexpected behavior. Example: Two Teams, Same Function Name Let's demonstrate with two JavaScript files from different teams: TeamA1.js Team A1 creates a student constructor with 2 parameters: function student(fname, lname) ...
Read MoreHow to remove html tags from a string in JavaScript?
Removing HTML tags from strings is a common task in JavaScript web development. You can accomplish this using regular expressions to match and replace HTML tag patterns with empty strings. Understanding HTML Tag Structure HTML elements are enclosed between angle brackets like , , , etc. By targeting the pattern of content within these brackets, we can effectively strip all HTML tags from a string. Syntax str.replace(/(]+)>)/ig, ''); The regular expression /(]+)>)/ig breaks down as: ]+) - matches one or more characters that aren't closing brackets > - matches closing angle bracket ...
Read MoreHow to subtract days from a date in JavaScript?
In JavaScript, you can subtract days from a date using built-in Date methods. The most common approaches are using setTime() with getTime() for millisecond-based calculation, or setDate() with getDate() for simpler day-based arithmetic. Following are the methods we can use to subtract days from a date in JavaScript: Using the setTime() and getTime() methods Using the setDate() and getDate() methods Using setTime() and getTime() Methods The setTime() method sets a date by milliseconds since ...
Read More