Found 6710 Articles for Javascript

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

678 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

779 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

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

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

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

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

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

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

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

How to calculate the XOR of array elements using JavaScript?

AmitDiwan
Updated on 16-Feb-2023 11:57:39

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. Array XOR The XOR (exclusive ... Read More

How to calculate the date three months prior using JavaScript?

AmitDiwan
Updated on 16-Feb-2023 11:55:16

2K+ Views

To calculate the date three months prior using JavaScript, we will first need to create a new date object, which will represent the current date. We will then use the setMonth() method to subtract 3 from the current month. Finally, we will convert this new date object back to a string using the toString method to display the date three months prior. Basically, we will be writing a dynamic JavaScript function that can take in a number as its input and return the date prior to that number of months from today’s date. For example − calculatePriorDate(4); This ... Read More

How to calculate and print bonus and gross using basic salary by JavaScript?

AmitDiwan
Updated on 16-Feb-2023 11:52:32

1K+ Views

We will first determine the bonus percentage by multiplying the basic salary by a certain percentage. Next, we will calculate the bonus amount by multiplying the bonus percentage with the basic salary. Finally, we will add the bonus amount to the basic salary to determine the gross salary and print all three values. Here is an example of how to calculate and print the bonus and gross salary using the basic salary in JavaScript − // Declare basic salary variable var basicSalary = 5000; // Calculate bonus (10% of basic salary) var bonus = basicSalary * 0.1; // ... Read More

Advertisements