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 445 of 534
Object initializer in JavaScript
An object initializer is an expression that allows us to initialize a newly created object. It is a comma-separated list of zero or more pairs of property names and associated values of an object, enclosed in a pair of curly braces {}. Basic Syntax const objectName = { property1: value1, property2: value2, // ... more properties }; Simple Object Creation Here's a basic example of creating an object using object initializer syntax: ...
Read MoreThe new.target in JavaScript
The new.target is a metaproperty that allows us to determine at runtime whether a function or constructor was called using the new keyword or not. It returns undefined when called as a regular function and references the constructor when called with new. Basic Syntax function MyConstructor() { if (new.target) { // Called with 'new' } else { // Called as regular function } } Example: Detecting Constructor ...
Read MoreThe yield* expression/keyword in JavaScript.
The yield* expression in JavaScript is used to delegate to another generator function or any iterable object. It yields all values from the delegated generator or iterable, effectively "flattening" nested generators. Syntax function* generatorFunction() { yield* anotherGenerator(); yield* iterableObject; } Basic Example: Delegating to Another Generator yield* Example yield* keyword in JavaScript CLICK ...
Read MoreThe debugger statement in JavaScript
The debugger statement in JavaScript is used to set a breakpoint in your code. When the JavaScript engine encounters this statement, it pauses execution and opens the browser's developer tools debugger (if available), allowing you to inspect variables, step through code, and debug issues. Syntax debugger; How It Works When the JavaScript engine hits a debugger statement: Execution pauses at that line Browser developer tools open automatically You can inspect the current scope, variables, and call stack If no debugger is available, the statement is ignored Example: Basic Debugging ...
Read MoreSorting JavaScript object by length of array properties.
In JavaScript, you can sort objects by the length of their array properties using the sort() method with a custom comparison function. This is useful when organizing data based on array sizes. Syntax array.sort((a, b) => a.property.length - b.property.length); Example: Sorting Students by Number of Subjects Sort by Array Length body { ...
Read MoreHow to multiply two Arrays in JavaScript?
In JavaScript, multiplying two arrays means performing element-wise multiplication, where each element at the same index in both arrays is multiplied together. This creates a new array with the products. Element-wise Array Multiplication When multiplying arrays, we iterate through corresponding indices and multiply the values at those positions: Array Multiplication Multiply Two Arrays Multiply Arrays ...
Read MoreExplain 'Not a constructor function' error in JavaScript?
The "Not a constructor function" error occurs when we try to use the new keyword with a value that isn't a constructor function. This TypeError is thrown when JavaScript expects a constructor but receives a primitive value, object, or non-constructor function. What Causes This Error The error occurs when we attempt to instantiate something that cannot be used as a constructor: Primitive values (numbers, strings, booleans) Regular objects Arrow functions (in strict mode) Built-in methods that aren't constructors Example: Using Primitive as Constructor Constructor Error Demo ...
Read MoreHow to de-structure an imported object in JavaScript?
Destructuring allows you to extract specific properties from imported objects in JavaScript modules. This is particularly useful when you only need certain properties from a larger object. Basic Destructuring Syntax When importing an object, you can destructure it immediately or after import: // Method 1: Destructure after import import person from "./sample.js"; let {firstName, lastName, age} = person; // Method 2: Direct destructuring (named exports) import {firstName, lastName, age} from "./sample.js"; Example: Complete Implementation sample.js (Module file) export default { firstName: 'Rohan', ...
Read MoreHow to assign static methods to a class in JavaScript?
To assign static methods to a class in JavaScript, prefix the method with the static keyword. Static methods belong to the class itself rather than instances and can be called without creating an object. Syntax class ClassName { static methodName() { // method body } } // Call static method ClassName.methodName(); Basic Static Method Example class Calculator { static add(a, b) { return a ...
Read MoreSetting property in an empty object using for loop in JavaScript.
In JavaScript, you can populate an empty object with properties using various loop methods. The most common approaches are the for...in loop and the for...of loop with Object.entries(). Using for...in Loop The for...in loop iterates over all enumerable properties of an object, making it ideal for copying properties from one object to another. Setting Properties with for...in Loop Setting Properties in Empty Object Populate ...
Read More