Web Development Articles

Page 431 of 801

How to create a constant array in JavaScript? Can we change its values? Explain.

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

To create a const array in JavaScript, we use the const keyword before the array name. While the array reference cannot be reassigned, individual elements can be modified. A const array prevents reassignment of the entire array but allows mutation of its contents. This is because const creates an immutable binding, not an immutable value. Syntax const arrayName = [element1, element2, element3]; Example: Creating and Modifying a Const Array Const Array Example ...

Read More

Get number from user input and display in console with JavaScript

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

Getting user input numbers in JavaScript involves capturing values from HTML input elements and converting them to numeric types for processing. This tutorial shows how to get a number from user input and display it in the console. HTML Structure First, create the HTML form with an input field and button: Get Number Input Number Input Example ...

Read More

Add oninput attribute to HTML element with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 659 Views

You can add the oninput attribute to HTML elements using JavaScript in two ways: using addEventListener() or directly setting the oninput property. Both methods allow you to dynamically attach input event handlers to elements. Using addEventListener() (Recommended) The addEventListener() method is the modern approach for attaching event listeners: Add oninput with addEventListener Enter the value: ...

Read More

Segregate all 0s on right and 1s on left in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 296 Views

We have an array of Numbers that contains 0, 1 and some other numbers. We are required to write a JavaScript function that takes in this array and brings all 1s to the start and 0s to the end Let's write the code for this function − Example const arr = [3, 2, 1, 8, 9, 0, 1, 9, 0, 2, 1, 0, 2, 0, 1, 0, 1, 1, 4, 0, 3]; const segregate = arr => { const copy = arr.slice(); for(let i = 0; i ...

Read More

How to stop a function during its execution in JavaScript?

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

In JavaScript, you can stop a function during execution using different techniques depending on the function type. For repeating functions like setInterval(), use clearInterval(). For one-time delayed functions, use clearTimeout(). Stopping setInterval() Functions The most common scenario is stopping a repeating function created with setInterval(): Stop Function Execution Start Function Stop Function ...

Read More

Range Overflow and Range Underflow properties in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 811 Views

In JavaScript, the rangeOverflow and rangeUnderflow properties are part of the ValidityState object, used to check if form input values exceed their specified ranges. Range Underflow The rangeUnderflow property returns true if the element's value is less than the value specified in the min attribute. Range Overflow The rangeOverflow property returns true if the element's value is greater than the value specified in the max attribute. Syntax element.validity.rangeOverflow // true if value > max element.validity.rangeUnderflow // true if value < min Example ...

Read More

Wildcard matching of string JavaScript

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

We are required to write a JavaScript function that accepts two strings and a number n. The function matches the two strings i.e., it checks if the two strings contains the same characters. The function should return true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, otherwise the function should return false. Let's write the code for this function — Example const str1 = 'first string'; const str2 = 'second string'; const wildcardMatching = (first, second, num) => { ...

Read More

Add class (odd and even) in HTML via JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 681 Views

JavaScript provides several ways to add CSS classes to HTML elements based on odd and even positions. You can use CSS nth-child selectors or JavaScript to dynamically add classes to elements. Method 1: Using CSS nth-child Selectors The simplest approach is using CSS nth-child(odd) and nth-child(even) selectors to style elements directly: Odd Even Classes .subjectName:nth-child(odd) { ...

Read More

How to add, access, delete, JavaScript object properties?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 206 Views

JavaScript objects are collections of key-value pairs where you can dynamically add, access, and delete properties. This guide demonstrates the fundamental operations for managing object properties. Syntax // Adding properties obj.propertyName = value; obj['propertyName'] = value; // Accessing properties obj.propertyName; obj['propertyName']; // Deleting properties delete obj.propertyName; delete obj['propertyName']; Example: Adding Properties Object Properties Add, Access, Delete JavaScript Object Properties Manage Properties function manageProperties() { let obj = { firstName: ...

Read More

Separate odd and even in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 827 Views

In JavaScript, separating odd and even numbers means rearranging an array so that all even numbers appear before all odd numbers. This is commonly achieved using custom sorting functions or array methods like filter(). Using Custom Sort Function The most efficient approach uses Array.sort() with a custom comparator that prioritizes even numbers: const arr = [2, 6, 3, 7, 8, 3, 5, 4, 3, 6, 87, 23, 2, 23, 67, 4]; const isEven = num => num % 2 === 0; const sorter = (a, b) => { if(isEven(a) && !isEven(b)){ ...

Read More
Showing 4301–4310 of 8,010 articles
« Prev 1 429 430 431 432 433 801 Next »
Advertisements