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 432 of 534
How to declare Block-Scoped Variables in JavaScript?
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 MoreObject.fromEntries() method in JavaScript.
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 MoreExplain Optional Catch Binding in JavaScript.
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 MoreObject de-structuring in JavaScript.
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 MoreGet global variable dynamically by name string in JavaScript?
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 MoreHow to make Format ABC-1234 in JavaScript regular Expressions?
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 MoreThe onchange event is not working in color type input with JavaScript
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 MoreGet value from div with JavaScript resulting undefined?
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 MoreHow to override derived properties in JavaScript?
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 MoreLooping over matches with JavaScript regular expressions
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