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 by vineeth.mariserla
Page 4 of 8
What is function chaining in JavaScript?
Function chaining is a technique that allows you to call multiple methods on the same object in sequence using dot notation. This makes code more concise and improves readability by eliminating the need to repeatedly reference the same object. How Function Chaining Works For function chaining to work, each method must return the object itself (typically using return this). This allows the next method in the chain to be called on the returned object. Without Function Chaining In this example, the methods don't return this, so chaining is not possible: ...
Read MoreHow to find duplicates in an array using set() and filter() methods in JavaScript?
Finding duplicates in JavaScript arrays is a common task that can be accomplished using modern JavaScript methods. The Set() constructor and filter() method provide elegant solutions for removing duplicates without complex logic. Using Set() Method The Set() constructor automatically stores only unique values, making duplicate removal straightforward. When combined with the spread operator, it creates a new array with duplicates removed. Syntax let uniqueArray = [...new Set(originalArray)]; Example var dupNames = ['John', 'Ram', 'Rahim', 'Remo', 'Ram', 'Rahim']; var uniArr = [...new ...
Read MoreExplain in detail about the memory life cycle of JavaScript?
JavaScript's memory management is crucial for application performance. Understanding the memory lifecycle helps developers write more efficient code and avoid memory leaks. The Three Stages of Memory Lifecycle Every programming language follows a similar memory lifecycle with three fundamental stages: Allocation of memory - Reserve memory space for variables and objects Use the allocated memory - Read from and write to the allocated memory Release the allocated memory - Free up memory when it's no longer needed In low-level languages, developers manually handle allocation and deallocation. However, high-level languages like JavaScript manage this process ...
Read MoreHow to access a JavaScript object using its own prototype?
JavaScript's Object.create() method creates a new object with the specified object as its prototype. This allows the new object to inherit properties from the existing object while maintaining a prototype chain. Syntax Object.create(prototypeObject); This method takes an existing object and creates a new object that inherits properties from it through the prototype chain. How It Works When you create an object using Object.create(), the new object doesn't copy the properties directly. Instead, it creates a prototype link to the original object, allowing property inheritance. var ...
Read MoreHow to execute a cube of numbers of a given array in JavaScript?
To calculate the cube of each number in an array, you can use several approaches. The most common methods involve iterating through the array and applying the cube operation (n³) to each element. Using a for Loop The traditional approach uses a for loop to iterate through the array and replace each element with its cube: var numbers = [1, 2, 3, 4, 5]; // Calculate cube of each element for (var i = 0; i ...
Read MoreHow to hide/show HTML elements in JavaScript?
JavaScript provides several ways to hide and show HTML elements by manipulating their CSS properties. The most common approach is using the display property with values like none (to hide) and block (to show). Hiding an Element To hide an element, set its display property to none. This removes the element from the document flow completely. Example In the following example, clicking the "Hide Me" button hides the paragraph text: Using JavaScript to hide HTML elements. Showing an Element ...
Read MoreHow to get a part of string after a specified character in JavaScript?
To get a part of a string after a specified character in JavaScript, you can use the substring() method combined with indexOf(). This technique allows you to extract portions of text before or after any character. Understanding substring() The substring() method extracts characters from a string between two specified indices. It takes a start index (inclusive) and an optional end index (exclusive). Syntax for Getting Text After a Character string.substring(string.indexOf(character) + 1); Here, indexOf(character) finds the position of the character, and adding + 1 starts extraction from the next position. Syntax for ...
Read MoreWhat 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 MoreRegular functions vs Arrow functions in JavaScript?
Arrow functions and regular functions are both ways to define functions in JavaScript, but they have important differences in behavior. Understanding these differences helps you choose the right function type for your needs. Syntax The syntax differs between regular and arrow functions: Arrow Function Syntax let x = (params) => { // code }; Regular Function Syntax let x = function functionName(params) { // code }; Usage of "this" Keyword The most significant difference is how they handle the this ...
Read More