Web Development Articles

Page 451 of 801

Accessing an array returned by a function in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 883 Views

In JavaScript, functions can return arrays that can be accessed and manipulated directly. This is useful for creating reusable code that generates data structures. Basic Syntax function functionName() { let array = [/* elements */]; return array; } // Access the returned array let result = functionName(); Example: Returning and Accessing Arrays Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...

Read More

How to format JSON string in JavaScript?

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

To format JSON string in JavaScript, use JSON.stringify() with spacing parameters. This method converts JavaScript objects to formatted JSON strings with proper indentation for better readability. Syntax JSON.stringify(value, replacer, space) Parameters value - The JavaScript object to convert replacer - Function or array to filter properties (use null for all properties) space - Number of spaces or string for indentation Example var details = { studentId: 101, studentFirstName: 'David', studentLastName: 'Miller', studentAge: 21, subjectDetails: { ...

Read More

Object initializer in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 369 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 228 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 186 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

How to subtract date from today's date in JavaScript?

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

To subtract dates in JavaScript, you can work with Date objects directly or extract specific components like day, month, or year. The most common approach is to subtract one Date object from another to get the difference in milliseconds. Syntax var dateDifference = date1 - date2; // Returns difference in milliseconds var daysDifference = Math.floor((date1 - date2) / (1000 * 60 * 60 * 24)); Example 1: Subtracting Full Dates var currentDate = new Date(); var pastDate = new Date("2024-01-01"); // Get difference in milliseconds var timeDifference = currentDate - pastDate; console.log("Difference ...

Read More

The debugger statement in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 868 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 535 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 397 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
Showing 4501–4510 of 8,010 articles
« Prev 1 449 450 451 452 453 801 Next »
Advertisements