Javascript Articles

Page 432 of 534

How to declare Block-Scoped Variables in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 273 Views

Block-scoped variables are declared using let and const keywords introduced in ES2015. Unlike var, these variables are only accessible within their containing block. Block scope means the variable exists only within the nearest enclosing curly braces {}. Syntax let variableName = value; const constantName = value; Example: Basic Block Scope Block Scoped Variables Block Scoped Variables Demo Test Block Scope ...

Read More

Object.fromEntries() method in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

The Object.fromEntries() method in JavaScript converts an iterable of key-value pairs (like arrays or Maps) into an object. It's the reverse operation of Object.entries(). Syntax Object.fromEntries(iterable) Parameters iterable: An iterable containing key-value pairs, such as an array of arrays or a Map. Return Value Returns a new object with properties derived from the key-value pairs in the iterable. Example: Converting Array of Arrays Object.fromEntries() Example Object.fromEntries() ...

Read More

Explain Optional Catch Binding in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 501 Views

The optional catch binding introduced in ES2019 allows us to omit the error parameter in catch blocks. Instead of catch(error), we can simply write catch when we don't need to access the error object. This feature is useful when we know the type of error in advance or want to handle errors without examining their details. Syntax Comparison Traditional catch binding requires parentheses and a parameter: try { // code that might throw } catch (error) { // handle error using 'error' parameter } Optional ...

Read More

Object de-structuring in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 159 Views

Object destructuring is a JavaScript feature that allows you to extract multiple properties from an object and assign them to variables in a single statement. It provides a clean and concise way to work with object properties. Syntax const { property1, property2, property3 } = object; Basic Example Object Destructuring Object Destructuring Example ...

Read More

Get global variable dynamically by name string in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 531 Views

In JavaScript, you can access global variables dynamically using their name as a string through the window object (in browsers) or global object (in Node.js). Syntax // Browser environment window[variableName] // Node.js environment global[variableName] // Using globalThis (works in both) globalThis[variableName] Basic Example Dynamic Global Variables // Define global variables ...

Read More

How to make Format ABC-1234 in JavaScript regular Expressions?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 345 Views

In JavaScript, you can format strings to match the pattern ABC-1234 using regular expressions. This pattern consists of three uppercase letters followed by a dash and four digits. Understanding the Pattern The ABC-1234 format requires: Three uppercase letters (A-Z) A dash (-) Four digits (0-9) Regular Expression Pattern The regex pattern for ABC-1234 format is: /^[A-Z]{3}-\d{4}$/ Breaking down this pattern: ^ - Start of string [A-Z]{3} - Exactly 3 uppercase letters - - Literal dash character \d{4} - Exactly 4 digits $ - End of string Method ...

Read More

The onchange event is not working in color type input with JavaScript

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

The onchange event works perfectly with color input elements in JavaScript. When a user selects a different color, the event triggers automatically, allowing you to capture and process the new color value. Syntax Basic Example Color Input onchange Event Choose Color: ...

Read More

Get value from div with JavaScript resulting undefined?

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

When trying to get the value from a div element using JavaScript, you might encounter undefined results if you use incorrect properties. The key is understanding the difference between innerHTML, textContent, and value. The Problem The most common mistake is using .value on div elements. The value property only works for form inputs like , , and . For div elements, use innerHTML or textContent. Using innerHTML innerHTML gets the HTML content inside the element, including any HTML tags: Get Div Value ...

Read More

How to override derived properties in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 390 Views

In JavaScript, when an object inherits properties from its prototype, you can override those inherited properties by assigning new values directly to the child object. This creates own properties that shadow the prototype properties. Understanding Property Inheritance When you create an object using Object.create(), the new object inherits properties from its prototype. These inherited properties can be overridden by setting properties directly on the derived object. Example: Overriding Derived Properties Override Derived Properties ...

Read More

Looping over matches with JavaScript regular expressions

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 949 Views

In JavaScript, you can loop over regular expression matches using several methods. The most common approaches are using match() with the global flag, matchAll(), or exec() in a loop. Using match() with Global Flag The match() method with the global flag (g) returns an array of all matches, which you can then iterate over: Regex Matches Loop body { ...

Read More
Showing 4311–4320 of 5,340 articles
« Prev 1 430 431 432 433 434 534 Next »
Advertisements