Web Development Articles

Page 487 of 801

Sleep in JavaScript delay between actions?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 529 Views

To create a sleep or delay in JavaScript, use setTimeout() for simple delays or async/await with Promises for more control. JavaScript doesn't have a built-in sleep function like other languages. 1000 milliseconds = 1 second 2000 milliseconds = 2 seconds, etc. Using setTimeout() for Delays setTimeout() executes a function after a specified delay. Here's an example with a 3-second delay: console.log("Starting calculation..."); setTimeout(function() { var firstValue = 10; var secondValue = 20; var result = firstValue + secondValue; ...

Read More

Extract key value from a nested object in JavaScript?

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

Extracting keys from nested objects is a common requirement in JavaScript. This tutorial shows how to find the parent object and nested property that contains a specific value. Creating a Nested Object Let's start with a nested object containing teacher and subject information: var details = { "teacherDetails": { "teacherName": ["John", "David"] }, "subjectDetails": { "subjectName": ["MongoDB", "Java"] } } console.log("Nested object created:", ...

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

How to add properties from one object into another without overwriting in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 700 Views

When merging objects in JavaScript, you often want to add properties from one object to another without overwriting existing values. This preserves the original object's data while incorporating new properties. The Problem Consider these two objects where some properties overlap: var first = {key1: 100, key2: 40, key3: 70}; var second = {key2: 80, key3: 70, key4: 1000}; console.log("First object:", first); console.log("Second object:", second); First object: { key1: 100, key2: 40, key3: 70 } Second object: { key2: 80, key3: 70, key4: 1000 } We want to add key4 from ...

Read More

Update array of objects with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 498 Views

In JavaScript, you can update an array of objects by modifying existing objects or adding new ones. This is commonly done using methods like push(), splice(), or array methods like map() and find(). Let's say we have the following array of objects: var studentDetails = [ { firstName: "John", listOfSubject: ['MySQL', 'MongoDB']}, {firstName: "David", listOfSubject: ['Java', 'C']} ]; console.log("Initial array:", studentDetails); Initial array: [ { firstName: 'John', listOfSubject: [ 'MySQL', 'MongoDB' ] }, { firstName: 'David', listOfSubject: [ 'Java', 'C' ] } ] ...

Read More

Replace HTML div into text element with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 889 Views

To replace HTML div content with text from another element, you can use document.querySelectorAll() to select multiple elements and getElementsByClassName() to get the source text. This approach allows you to dynamically update multiple elements with content from a single source. Example Replace Div Content My Name is John ...

Read More

Is there a DOM function which deletes all elements between two elements in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 370 Views

In JavaScript, there's no single DOM function that directly deletes all elements between two elements. However, you can achieve this using a combination of DOM methods like nextElementSibling and remove(). Problem Setup Consider the following HTML structure where we want to remove all elements between a starting point and an ending point: START My Name is John My Name is David My Name is Bob My Name is Mike My Name is Carol END We need to remove all elements between the (START) and (END) elements. Solution Using nextElementSibling ...

Read More

Strip quotes with JavaScript to convert into JSON object?

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

When dealing with JSON strings that have escaped quotes or extra quote wrapping, you need to clean them before parsing. This article shows how to strip unwanted quotes and convert the cleaned string into a JavaScript object. The Problem Sometimes JSON data comes with extra quotes or escaped quote characters that prevent direct parsing. Here's a common scenario where a JSON string is wrapped in extra quotes and has doubled internal quotes: var studentDetails = `"{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}"`; console.log("Original string:"); console.log(studentDetails); Original string: "{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}" ...

Read More

Converting any string into camel case with JavaScript removing whitespace as well

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

In JavaScript, camel case formatting converts strings by lowercasing the first letter and capitalizing the first letter of each subsequent word, while removing all whitespace. What is Camel Case? Camel case is a naming convention where the first word starts with a lowercase letter and each subsequent word starts with an uppercase letter, with no spaces or punctuation. For example: "hello world" becomes "helloWorld". Using Regular Expression Method The most efficient approach uses a regular expression with the replace() method to transform the string: function convertStringToCamelCase(sentence) { return sentence.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, ...

Read More

How to create a custom function similar to find() method in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 469 Views

Let's say we have the following records of studentId and studentName and want to check a specific student name: const studentDetails = [ { studentId: 101, studentName: "John" }, { studentId: 102, studentName: "David" }, { studentId: 103, ...

Read More
Showing 4861–4870 of 8,010 articles
« Prev 1 485 486 487 488 489 801 Next »
Advertisements