Found 6685 Articles for Javascript

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

AmitDiwan
Updated on 16-Feb-2023 13:52:21

56 Views

In this article, you will understand how Promise.any() method differs from Promise.race() method in JavaScript. The Promise.any() method in javascript is one among promise concurrency methods. It is useful when the first task needs to be completed. The Promise.race() method in javascript is one among promise concurrency methods. It is useful when the first async task need to be complete, but do not care about its eventual state (i.e. it can either succeed or fail). Example 1 In this example, let’s look at how the Promise.any() method works console.log("Defining three promise values: promise1, promise2 and promise3"); const promise1 = ... Read More

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

AmitDiwan
Updated on 16-Feb-2023 12:44:54

109 Views

In this article, you will understand how does Promise.all() method differs from the Promise.allSettled() method in JavaScript. The Promise.all() method takes in one or multiple promises as input and returns a single Promise. This returned promise fulfills when all of the input's promises are fulfilled. It rejects a promise when any of the input's promises is rejected, with this first rejection reason. The Promise.allSettled() method takes in one or multiple promises as input and returns a single Promise. This returned promise fulfills when all of the input's promises settle (including when an empty iterable is passed), with an array of ... Read More

How does internationalization work in JavaScript?

AmitDiwan
Updated on 16-Feb-2023 12:45:33

200 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. Example 1 In this example, let’s understand the changing of date and time formats. var inputDate = new Date(1990, 2, 25); console.log("The date is defined as: ") console.log(inputDate) console.log("The date in United Kingdom format is: ") console.log(new Intl.DateTimeFormat('en-GB').format(inputDate)); console.log("The date in American format is: ") console.log(new Intl.DateTimeFormat('en-US').format(inputDate)); Explanation Step 1 − Define ... Read More

How does inline JavaScript work with HTML?

AmitDiwan
Updated on 16-Feb-2023 12:30:02

5K+ 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 a html file. The advantage of using inline JavaScript in HTML files is to reduce the round trip of the web browser to the server. Example 1 Let us understand the how to add a basic statement a html file using inline JavaScript − This is a simple HTML file document.write("Hi, This is an inline Javascript code written ... Read More

How does Implicit coercion differ from Explicit coercion in JavaScript?

AmitDiwan
Updated on 16-Feb-2023 12:46:53

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. It is also known as type conversion. An explicit coercion is the conversion of data type depending on the user's necessity. Example 1 In this example, let us learn about implicit coercion. let inputValue = "5" console.log("The input variable is defined as: ") console.log(inputValue, typeof inputValue); let resultValue = Number(inputValue); console.log("The input variable is defined as: ") console.log(resultValue, typeof resultValue); Explanation Step 1 −Define a variable: inputValue and assign ... Read More

How Check if object value exists not add a new object to array using JavaScript ?

AmitDiwan
Updated on 16-Feb-2023 12:47:37

1K+ Views

In this article, you will understand how to check if the object value exists, if not, add a new object to the array using JavaScript. In Javascript, almost every variable is an object. An object can be a string, numbers, boolean values, etc. They can also be key-value pairs. An array in javascript is a special variable that can hold more than one item. An array can be initialized using the keyword ‘const’. Example 1 In this example, we check the existence of the object using the .some() function. var inputArray = [{ id: 1, name: "JavaScript" }, ... Read More

How to remove falsy values from an array in JavaScript?

AmitDiwan
Updated on 16-Feb-2023 12:23:09

484 Views

In this article, you will understand how to remove false values from an array in JavaScript. An array in JavaScript is a special variable that can hold more than one item. An array can be initialized using the keyword ‘const’ .Falsy values are nothing but boolean false values. Example 1 The first way of achieving it is by iterating the array using a for-each loop and pushing the non-false values to another array and returning the array. const inputArray = [true, false, false, true, null, 505, 101]; console.log("The input array is defined as: ") console.log(inputArray) function removeFalsey(inputArray) { ... Read More

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

AmitDiwan
Updated on 16-Feb-2023 12:19:40

562 Views

We can use the "Object.keys()" method to retrieve the keys of an object. However, instead of returning the keys as a string, we can wrap the call to "Object.keys()" in a function. This way, when we call the function, it will return the keys as a method, rather than a string. Approach 1 You can use the Object.keys() method to get an array of the keys of an object, then use array notation or the [] operator to access the key as a property of the object. Here is an example − let obj = { key1: "value1", key2: ... Read More

How to call a function from its name stored in a string using JavaScript?

AmitDiwan
Updated on 16-Feb-2023 12:12:39

8K+ Views

We will use the eval() function to call a function from its name stored in a string. The eval() function evaluates a string as JavaScript code. This allows us to call the function by passing the string containing its name as an argument to the eval() function. Example Here is a complete working example of how to call a function from its name stored in a string using JavaScript − // Define the function that we want to call function sayHello() { console.log("Hello, world!"); } // Store the function name as a string let functionName ... Read More

How to call a function that returns another function in JavaScript?

AmitDiwan
Updated on 10-Sep-2023 08:04:26

35K+ Views

We will call a function by referencing its name and adding parentheses after it. If the function we are calling returns another function (it does in our case), we will need to assign it to a variable or call it immediately. In the future, we will need to make sure that we understand the behavior of the returned function and how it can be utilized in our code. This is call Function Currying. Function Currying Function currying is a technique in functional programming where a function is transformed into a sequence of functions, each taking a single ... Read More

Advertisements