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
Web Development Articles
Page 451 of 801
Accessing an array returned by a function in JavaScript
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 MoreHow to format JSON string in JavaScript?
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 MoreObject 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 MoreHow to subtract date from today's date in JavaScript?
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 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 More