Found 6710 Articles for Javascript

How to use map() on an array in reverse order with JavaScript?

Shubham Vora
Updated on 16-Feb-2023 17:00:23

4K+ Views

We have given the array of elements and need to use the map() on the array in reverse order with JavaScript. The map() method maps the new element related to a particular array element in the resultant array. Sometimes, we require to map the elements in reverse order. There can be many ways to use map() on an array in reverse order. We will see all approaches one by one. Introduction to map() method Syntax Follow the syntax below to use the map() method. array.map((ele, ind, array) => { return ele + 20; }) ... Read More

How to use map and filter simultaneously on an array using JavaScript?

Shubham Vora
Updated on 16-Feb-2023 16:59:47

5K+ Views

The filter() method is used to filter values from the array, and the map() method is used to map a new value to another array based on every value of the old array. Sometimes, we require to use the filter() and map() methods together. For example, we wanted to filter all positive numbers from the array and map their logarithmic value to the new array Let’s look at the introduction of the filter() and map() methods before we start with this tutorial. In this tutorial, we will learn to use the map and filter methods simultaneously on an array ... Read More

How to use the JavaScript function in the check box?

Shubham Vora
Updated on 16-Feb-2023 16:58:35

14K+ Views

While working with the HTML checkboxes, sometimes developers may require to use the checkboxes to allow users to select multiple options. Also, developers need to perform some actions based on the selected checkbox. For example, you have given the checkbox with a question like are you above 18? If users check this checkbox, only show the input to take a user’s pan card number. So, we need to call a function whenever the user checks and unchecks the checkboxes. Use the onchange event The onchange event attribute allows us to invoke a function whenever the user checks and ... Read More

How to use JavaScript Fetch API to Get Data?

Shubham Vora
Updated on 16-Feb-2023 16:57:43

19K+ Views

Nowadays, JavaScript is useful for writing frontend and backend code. Also, it is the most widely used programming language. Furthermore, we need to get the data from other servers while developing real-time applications. We can fetch data from other servers or databases using the API, which stands for the application programming interface. Here, we will learn various methods to fetch API to get data using JavaScript. Use the fetch() method The fetch() is a browser’s method to get the data from the API. It takes the API URL as the first parameter we need to get data ... Read More

How to unpack array elements into separate variables using JavaScript?

Shubham Vora
Updated on 16-Feb-2023 16:56:35

954 Views

Unpacking array elements means assigning array element values to the new variables. We can assign every element to the separate variables, or we can assign some elements to the separate variables, and for the remaining elements, we can create a new array. In this tutorial, we will learn to unpack array elements into separate variables using JavaScript. Syntax Users can follow the syntax below to unpack the array elements into separate variables. let array = [element1, element2, element3, element4]; let [a, b, c, d] = array; In the above syntax, a variable contains the value of element1, ... Read More

How to trim a file extension from a string using JavaScript?

Shubham Vora
Updated on 16-Feb-2023 16:55:03

8K+ Views

Many web applications allow users to upload a file. Also, it shows the file content with the filename after trimming the file extension. Furthermore, sometimes we require to store the file's content in the database with the filename without extension as a key. So, various use cases require us to use the file name of the uploaded file without extension. Here, we will learn multiple ways to trim the file extension from the string using JavaScript. Use the array.split(), array.pop() and array.join() method Every file name contains the file extension after the last dot in the file name. ... Read More

How to Toggle an Element Class in JavaScript?

Shubham Vora
Updated on 16-Feb-2023 16:53:10

6K+ Views

Toggling an element class means adding and removing a particular class from the HTML element based on a certain condition. For example, we want to highlight the HTML div element, and when the mouse enters, we need to add a particular class with a different style in the HTML element. Here, we will learn various approaches to toggle an element class using JavaScript and jQuery. In this tutorial, we will learn to toggle an element class in JavaScript. Using the toggle() method of the classList The toggle() method toggles the class in the element. It checks if ... Read More

How to swap the key and value of JSON element using JavaScript?

Shubham Vora
Updated on 16-Feb-2023 16:51:52

3K+ Views

Here, we will learn to swap the key and value of the JSON element using JavaScript. In JavaScript, an object stores the key-value pairs. So, we can swap the key and value as we swap two normal variables. In this tutorial, we will learn different approaches to swapping all the key values of JSON elements using JavaScript. Use the for loop We can iterate through the keys of the JSON object using for loop. After that, we can access the value from the object using the key. So, we can swap every key and value in the object. ... Read More

How to store all dates in an array present in between given two dates in JavaScript?

Shubham Vora
Updated on 16-Feb-2023 16:50:40

3K+ Views

Sometimes, we require to get all the dates between the given date range. In this tutorial, we will take two dates and find all dates between two dates. Also, we will store all dates in the array. Here, we will learn three approaches to storing all dates in an array present between given two dates in JavaScript. Use the while loop and setDate() method We can use the while loop for iteration and the setDate() method to set dates in the date object. On every iteration of the while loop, we can increase the date by one ... Read More

How to store a key => value array in JavaScript?

Shubham Vora
Updated on 16-Feb-2023 16:49:31

6K+ Views

Sometimes, we require to map keys to particular values using some data structure in JavaScript. For example, storing the user details in the key value pairs in JavaScript is useful. We can use different data structures, such as objects or maps in JavaScript, to store data in the key-value format. Use the objects to store a key => value in JavaScript In JavaScript, objects allow us to store data in the key-value format. We can use the key with the object to get the data from the object. Syntax Users can follow the syntax below to use ... Read More

Advertisements