Javascript Articles

Page 229 of 534

How to calculate the XOR of array elements using JavaScript?

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

We will use a for loop to iterate through the array. We will initialize a variable called "result" with the value of the first element in the array. For each subsequent element in the array, we will use the XOR operator to update the value of "result" with that element. This process will continue until all elements in the array have been processed, resulting in the final XOR value of all elements in the array. Let us first understand what XOR is. We will also see how XOR operation on an array works. Understanding Array XOR ...

Read More

How to call a function repeatedly every 5 seconds in JavaScript?

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

In JavaScript, setInterval() allows you to execute a function repeatedly at specified time intervals. To call a function every 5 seconds, you pass the function and 5000 milliseconds as arguments. Syntax setInterval(function, delay); Where function is the function to execute and delay is the time in milliseconds between executions. Basic Example function myFunction() { console.log("Function called at:", new Date().toLocaleTimeString()); } // Call myFunction every 5 seconds (5000 milliseconds) setInterval(myFunction, 5000); Function called at: 2:30:15 PM Function called at: 2:30:20 PM Function called at: 2:30:25 ...

Read More

How to call the key of an object but return it as a method, not a string in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 860 Views

In JavaScript, you can call object methods dynamically using bracket notation instead of dot notation. This allows you to access and execute object methods using string keys, which is useful when the method name is determined at runtime. Basic Dynamic Method Access You can use bracket notation obj[key]() to call methods dynamically: const obj = { greet: function() { console.log("Hello!"); }, farewell: function() { console.log("Goodbye!"); ...

Read More

How to remove falsy values from an array in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 741 Views

In JavaScript, falsy values are values that evaluate to false in a boolean context. These include false, 0, "", null, undefined, and NaN. Removing falsy values from an array is a common operation that can be accomplished using several methods. What are Falsy Values? JavaScript has six falsy values that evaluate to false when used in boolean contexts: const falsyValues = [false, 0, "", null, undefined, NaN]; console.log("Falsy values:"); falsyValues.forEach(value => { console.log(`${value} is falsy:`, !value); }); Falsy values: false is falsy: true 0 is falsy: true is ...

Read More

How does inline JavaScript work with HTML?

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

In this article, you will understand how inline JavaScript works with HTML. Inline JavaScript represents a code block written in between the tags in an HTML file. The advantage of using inline JavaScript in HTML files is to reduce the round trip of the web browser to the server. What is Inline JavaScript? Inline JavaScript is JavaScript code that is embedded directly within HTML documents using tags. This approach allows you to execute JavaScript without creating separate .js files, making it convenient for small scripts and quick implementations. Example 1: Basic Inline JavaScript Let ...

Read More

How does Promise.all() method differs from Promise.allSettled() method in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 267 Views

In this article, you will understand how Promise.all() method differs from the Promise.allSettled() method in JavaScript. The Promise.all() method takes one or multiple promises as input and returns a single Promise. This returned promise fulfills when all of the input promises are fulfilled. It rejects immediately when any of the input promises is rejected, with this first rejection reason. The Promise.allSettled() method takes one or multiple promises as input and returns a single Promise. This returned promise fulfills when all of the input promises settle (either fulfilled or rejected), returning an array of objects that describe the outcome ...

Read More

How does internationalization work in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 352 Views

In this article, you will understand how internationalization works in JavaScript. Internationalization is the process of preparing software so that it can support local languages and cultural settings. It can include changing the date and time format, changing the metric system format, language format, etc. JavaScript provides the Intl object which contains constructors for locale-sensitive formatting and language-sensitive string comparison. The most commonly used are Intl.DateTimeFormat for dates and Intl.NumberFormat for numbers. Date and Time Formatting Let's understand how to format dates for different locales using Intl.DateTimeFormat: var inputDate = new Date(1990, 2, 25); console.log("The ...

Read More

How does Implicit coercion differ from Explicit coercion in JavaScript?

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

In this article, you will understand how implicit coercion differs from explicit coercion in JavaScript. An implicit coercion is an automatic conversion of values from one datatype to another that JavaScript performs automatically without programmer intervention. An explicit coercion is the deliberate conversion of data type using built-in functions or operators. Implicit Coercion JavaScript automatically converts data types when needed, especially in operations involving different types. let number = 5; let text = "10"; // JavaScript automatically converts number to string let result1 = number + text; console.log("5 + '10' =", result1, typeof result1); ...

Read More

How does Promise.any() method differs from Promise.race() method in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 184 Views

In this article, you will understand how Promise.any() method differs from Promise.race() method in JavaScript. The Promise.any() method resolves when the first promise succeeds (fulfills), ignoring rejections until all promises fail. The Promise.race() method settles when the first promise completes, regardless of whether it succeeds or fails. Promise.any() Method Promise.any() waits for the first successful promise and ignores rejections. If all promises reject, it throws an AggregateError. console.log("Defining three promise values: promise1, promise2 and promise3"); const promise1 = Promise.resolve(1); const promise2 = new Promise((resolve, reject) => { setTimeout(resolve, 200, 'Promise Two'); }); ...

Read More

How many numbers in the given array are less/equal to the given value using the percentile formula in Javascript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 242 Views

In this article, you will understand how to calculate the percentile of a given value in an array using JavaScript. The percentile tells us what percentage of numbers in the array are less than or equal to a specific value. Percentile Formula We use the following formula to calculate the percentile: Percentile = (n/N) * 100 Where: n = count of values less than or equal to the given value N = total number of values in the array For values equal to our target, ...

Read More
Showing 2281–2290 of 5,340 articles
« Prev 1 227 228 229 230 231 534 Next »
Advertisements