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

675 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

778 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

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

Calculate 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

Calculate 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

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

Build a Simple Terminal-like Website using jQuery

AmitDiwan
Updated on 16-Feb-2023 11:50:18

2K+ Views

We will start by creating a basic HTML structure for our website. Next, we will use jQuery to create a terminal-like interface by adding elements such as input fields and output displays. Finally, we will use jQuery functions to handle user input and display the appropriate response. Let us first understand what jQuery is. jQuery is a JavaScript library that simplifies HTML document traversing, event handling, and animation. It allows for easy manipulation of the Document Object Model (DOM). It also provides several shorthand methods for common JavaScript tasks, such as making AJAX requests. It is widely used in web ... Read More

Advertisements