Check If Object Value Exists and Not Add New Object to Array in 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

Implicit vs 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

Internationalization in JavaScript

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

285 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

Difference Between Promise.all and Promise.allSettled in JavaScript

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

189 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 Inline JavaScript Works with HTML

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

6K+ 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

Remove Falsy Values from an Array in JavaScript

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

686 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

Call Object Key as Method in JavaScript

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

792 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

Call Loading Function with React useEffect

AmitDiwan
Updated on 16-Feb-2023 12:14:36

14K+ Views

We will use the React useEffect hook to call our loading function. This hook allows us to specify a function that will run on a specific component lifecycle event, such as when the component mounts. By passing in our loading function as a dependency, we ensure that it will be called whenever the component loads or updates. React useEffect The useEffect is a Hook in React that allows you to synchronize a component with an external system. It runs after the component renders and can be used to fetch data, update the DOM, or set up event listeners. It ... Read More

Call a Function from Its Name Stored in a String Using JavaScript

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

10K+ 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

Call a Function Repeatedly Every 5 Seconds in JavaScript

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

31K+ Views

We will use the setInterval() function to repeatedly call a function every 5 seconds. This function takes two arguments, the first being the function to call and the second being the interval time in milliseconds. JavaScript setInterval setInterval() is a JavaScript function that allows you to execute a function repeatedly at a specified interval (in milliseconds). It returns a unique ID that can be used to clear the interval with the clearInterval() method. It can be useful for tasks such as periodically updating a page or creating animations. It takes two arguments, the function to be executed, and the ... Read More

Advertisements