Javascript Articles

Page 445 of 534

Object initializer in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 360 Views

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 More

The new.target in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 219 Views

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 More

The yield* expression/keyword in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 178 Views

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 More

The debugger statement in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 864 Views

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 More

Sorting JavaScript object by length of array properties.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 524 Views

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 More

How to multiply two Arrays in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

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 More

Explain 'Not a constructor function' error in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 387 Views

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 More

How to de-structure an imported object in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 215 Views

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 More

How to assign static methods to a class in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 786 Views

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 More

Setting property in an empty object using for loop in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 503 Views

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
Showing 4441–4450 of 5,340 articles
« Prev 1 443 444 445 446 447 534 Next »
Advertisements