AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 400 of 840

JavaScript array.values()

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 212 Views

The array.values() method returns an iterator object that contains all the values of an array. This method provides a way to iterate through array values using for...of loops or iterator methods. Syntax array.values() Parameters This method takes no parameters. Return Value Returns an Array Iterator object containing the values of the array. Example: Using array.values() with for...of Loop Array Values Example JavaScript array.values() ...

Read More

JavaScript code to de-select text on HTML page.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 195 Views

In JavaScript, you can deselect or clear text selections on an HTML page using the window.getSelection().removeAllRanges() method. This is useful for creating interactive interfaces where you want to programmatically clear user text selections. The Selection API The Selection API provides methods to work with text selections in the browser. The window.getSelection() object represents the current text selection, and removeAllRanges() clears all selected ranges. Syntax window.getSelection().removeAllRanges(); Example Deselect Text Example ...

Read More

Display array items on a div element on click of button using vanilla JavaScript

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

To display array items in a div element when a button is clicked, we need to iterate through the array and append each element to the target div. This is commonly done using JavaScript's forEach() method or a simple loop. Basic Approach The core concept involves selecting the target div using getElementById() and updating its innerHTML property with array elements: const myArray = ["stone", "paper", "scissors"]; const embedElements = () => { myArray.forEach(element => { document.getElementById('result').innerHTML += ...

Read More

How to disallow altering of object variables in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 189 Views

JavaScript provides Object.freeze() to make objects immutable, preventing addition, deletion, or modification of properties. This is useful when you need to protect object data from accidental changes. Syntax Object.freeze(object) Basic Example When an object is frozen, attempts to modify its properties are silently ignored: const canNotChangeTheFieldValueAfterFreeze = { value1: 10, value2: 20 }; Object.freeze(canNotChangeTheFieldValueAfterFreeze); // Attempt to change property (will be ignored) canNotChangeTheFieldValueAfterFreeze.value1 = 100; console.log("After changing the field value1 from 10 to 100 = " + canNotChangeTheFieldValueAfterFreeze.value1); ...

Read More

Split string into groups - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 324 Views

Given a string that consists of alphabets, numbers, and special characters, we need to split it into three separate strings based on character type. We'll create three strings where: S1 contains all alphabets from the original string S2 contains all numbers from the original string S3 contains all special characters from the original string The characters in each resulting string maintain the same order as they appear in the input string. Example Implementation const str = "Th!s String C0nt@1ns d1fferent ch@ract5rs"; const ...

Read More

JavaScript Immediately Invoked Function Expressions (IIFE)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 218 Views

An Immediately Invoked Function Expression (IIFE) is a JavaScript function that executes immediately after it has been defined. This pattern is useful for creating isolated scope and avoiding global namespace pollution. Syntax (function() { // Code here runs immediately })(); // Alternative syntax (function() { // Code here runs immediately }()); Basic IIFE Example IIFE Example JavaScript IIFE Demo ...

Read More

Select and Deselect Text Inside an Element using JavaScript

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

In JavaScript, you can programmatically select and deselect text inside DOM elements using the Selection API. This is useful for creating interactive features like highlighting content or implementing custom text selection controls. Core Methods The key methods for text selection are: window.getSelection().selectAllChildren(element) - Selects all text inside an element window.getSelection().removeAllRanges() - Clears all current selections Example Select and Deselect Text body { ...

Read More

Filtering of JavaScript object

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 284 Views

Filtering JavaScript objects allows you to extract specific key-value pairs based on certain criteria. This is useful when working with large datasets or when you need to display only relevant information. Problem Statement We need to create a function that takes an object and a search string, then filters the object keys that start with the search string and returns a new filtered object. Example: Basic Object Filtering const obj = { "PHY": "Physics", "MAT": "Mathematics", "BIO": "Biology", "COM": "Computer Science", "SST": "Social Studies", ...

Read More

Assign new value to item in an array if it matches another item without looping in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 162 Views

In JavaScript, you can assign a new value to an array item that matches a condition without using traditional loops. This can be achieved using array methods like filter() combined with map(), or more efficiently with forEach() or find(). Using filter() and map() The filter() method finds matching items, and map() modifies them. Here's how to change "Bob" to "Carol" in an array of objects: const studentDetails = [ {Name: "John"}, {Name: "David"}, {Name: "Bob"}, {Name: "Mike"} ]; var ...

Read More

Swap kth element of array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 679 Views

We are required to write a JavaScript function that accepts an array of Numbers and a number, say k (k must be less than or equal to the length of array). And our function should replace the kth element from the beginning with the kth element from the end of the array. Understanding the Problem When we say "kth element from beginning" and "kth element from end", we need to understand the positioning: kth element from beginning: index = k - 1 kth element from end: index = array.length - k Example ...

Read More
Showing 3991–4000 of 8,392 articles
« Prev 1 398 399 400 401 402 840 Next »
Advertisements