Web Development Articles

Page 455 of 801

Asynchronous Functions and the Node Event Loop in Javascript

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 217 Views

Asynchronous functions allow programs to continue executing without waiting for time-consuming operations to complete. This non-blocking behavior is fundamental to JavaScript and Node.js, enabling better user experience and efficient resource utilization. When executing expensive operations like network requests or file I/O, asynchronous functions prevent the entire program from freezing. Instead of blocking execution, these operations run in the background while other code continues to execute. Understanding Asynchronous Execution In synchronous programming, each operation must complete before the next one begins. Asynchronous programming allows multiple operations to run concurrently, with callbacks or promises handling completion. console.log('One'); ...

Read More

Is it possible to have JavaScript split() start at index 1?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 403 Views

The built-in String.prototype.split() method doesn't have a parameter to start splitting from a specific index. However, we can combine split() with array methods like slice() to achieve this functionality. Problem with Standard split() The standard split() method always starts from index 0 and splits the entire string: const text = "The quick brown fox jumped over the wall"; console.log(text.split(" ")); [ 'The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'wall' ] Method 1: Using split() with slice() The simplest ...

Read More

How to get sequence number in loops with JavaScript?

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

To get sequence numbers in loops with JavaScript, you can use various approaches. The most common method is maintaining a counter variable that increments with each iteration. Using forEach() with External Counter The forEach() method provides an elegant way to iterate through arrays while maintaining sequence numbers using an external counter: let studentDetails = [ { id: 101, details: [{name: 'John'}, {name: 'David'}, {name: 'Bob'}] }, { ...

Read More

Sorting arrays by two criteria in JavaScript

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

When sorting arrays by multiple criteria in JavaScript, you need to create a custom comparator function that handles each condition sequentially. This article demonstrates sorting objects by priority (isImportant) first, then by date within each priority group. The Array Structure Consider an array of objects with id, date, and isImportant properties. We want important items first, then sort by date within each group: const array = [{ id: 545, date: 591020824000, isImportant: false, }, { id: 322, ...

Read More

Split First name and Last name using JavaScript?

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

In JavaScript, you can split a full name into first and last name using the split() method. This method divides a string based on a specified separator and returns an array of substrings. Syntax string.split(separator) Basic Example var studentFullName = "John Smith"; var details = studentFullName.split(' '); console.log("Student First Name = " + details[0]); console.log("Student Last Name = " + details[1]); Student First Name = John Student Last Name = Smith Handling Multiple Names For names with more than two parts, you can extract the first ...

Read More

Sorting by 'next' and 'previous' properties (JS comparator function)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 433 Views

When working with linked data structures in JavaScript, you may encounter objects that reference each other through 'next' and 'previous' properties. This tutorial demonstrates how to sort such objects into their correct sequential order. Problem Statement Consider an array of objects representing pages of a website, where each object has: An id property for unique identification A next property pointing to the next page's id (unless it's the last page) A previous property pointing to the previous page's id (unless it's the first page) These objects are randomly positioned in the array, and we need ...

Read More

Replace commas with JavaScript Regex?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 764 Views

In JavaScript, you can replace commas in strings using the replace() method combined with regular expressions. This is particularly useful when you need to replace specific comma patterns, such as the last comma in a string. Problem Statement Consider these example strings with commas: "My Favorite subject is, " "My Favorite subject is, and teacher name is Adam Smith" "My Favorite subject is, and got the marks 89" We want to replace the last comma in each string with " JavaScript". Using Regular Expression to Replace Last Comma The regular expression /, ...

Read More

Convert object of objects to array in JavaScript

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

Let's say we have the following object of objects that contains rating of some Indian players, we need to convert this into an array of objects with each object having two properties namely name and rating where name holds the player name and rating holds the rating object. Following is our sample object: const playerRating = { 'V Kohli': { batting: 99, fielding: 99 }, 'R Sharma': { batting: 98, fielding: 95 }, ...

Read More

Add object to array in JavaScript if name does not already exist?

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

To add objects to an array only if they don't already exist, you can use push() combined with find() to check for duplicates. This prevents adding duplicate entries based on specific criteria. Basic Example Here's how to add objects to an array if the name property doesn't already exist: var details = [{name: "John"}, {name: "David"}]; var addObject = ["Mike", "Sam", "John"]; // "John" already exists addObject.forEach(obj1 => { if (!details.find(obj2 => obj2.name === obj1)) { details.push({name: obj1}); } ...

Read More

Stop making form to reload a page in JavaScript

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

When a form is submitted, browsers reload the page by default. To prevent this behavior and handle form submissions with JavaScript, we need to stop the default form submission event. The Problem HTML forms automatically reload the page when submitted. This interrupts JavaScript processing and user experience: Method 1: Using preventDefault() with Form Submit Event The most reliable approach is using event.preventDefault() in the form's submit event handler: ...

Read More
Showing 4541–4550 of 8,010 articles
« Prev 1 453 454 455 456 457 801 Next »
Advertisements