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
Javascript Articles
Page 467 of 534
Sleep in JavaScript delay between actions?
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 MoreExtract key value from a nested object in JavaScript?
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 MoreHow to disallow altering of object variables in JavaScript?
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 MoreHow to add properties from one object into another without overwriting in JavaScript?
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 MoreUpdate array of objects with JavaScript?
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 MoreReplace HTML div into text element with JavaScript?
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 MoreIs there a DOM function which deletes all elements between two elements in JavaScript?
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 MoreStrip quotes with JavaScript to convert into JSON object?
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 MoreConverting any string into camel case with JavaScript removing whitespace as well
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 MoreDifference between Hibernate and Eclipse link
Hibernate and EclipseLink are both Object-Relational Mapping (ORM) frameworks that implement the Java Persistence API (JPA) specification. While they serve the same fundamental purpose of mapping Java objects to relational databases, they have distinct differences in implementation and features. What is Hibernate? Hibernate is the most popular JPA implementation, developed by Red Hat. It provides robust ORM capabilities and includes additional features beyond the standard JPA specification. Hibernate has been widely adopted in enterprise applications due to its maturity and extensive documentation. What is EclipseLink? EclipseLink is an open-source JPA implementation developed by the Eclipse Foundation. ...
Read More