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 434 of 801
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 MoreSeparate a string with a special character sequence into a pair of substrings in JavaScript?
When you have a string containing a special character sequence that acts as a delimiter, you can separate it into substrings using JavaScript's split() method with regular expressions. Problem Statement Consider this string with a special character sequence: " John Smith " We need to split this string at the delimiter and get clean substrings without extra whitespace. Syntax var regex = /\s*\s*/g; var result = string.trim().split(regex); Example var fullName = " John Smith "; console.log("Original string: " + fullName); var regularExpression = ...
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 MoreIs it possible to display substring from object entries in JavaScript?
Yes, you can display substrings from object entries in JavaScript using Object.fromEntries() combined with string methods like substr() or substring(). This technique allows you to transform object keys while preserving their associated values. Syntax Object.fromEntries( Object.entries(object).map(([key, value]) => [key.substr(startIndex, length), value] ) ) Example: Extracting Substring from Object Keys const originalString = { "John 21 2010": 1010, "John 24 2012": 1011, "John 22 2014": ...
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 MoreRemove same values from array containing multiple values JavaScript
In JavaScript, arrays often contain duplicate values that need to be removed. The most efficient modern approach is using Set with the spread operator to create a new array with unique values only. Example Array with Duplicates Let's start with an array containing duplicate student names: const listOfStudentName = ['John', 'Mike', 'John', 'Bob', 'Mike', 'Sam', 'Bob', 'John']; console.log("Original array:", listOfStudentName); Original array: [ 'John', 'Mike', 'John', 'Bob', 'Mike', 'Sam', 'Bob', 'John' ] Using Set with Spread Operator (Recommended) The Set object automatically removes duplicates, and the spread operator converts it ...
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 More