Most Used Array Methods in JavaScript

Most Used Array Methods in JavaScript: A Comprehensive Overview As a JavaScript developer, you will frequently encounter arrays. Arrays are most familiar data structure in JavaScript, and its provide many functionality for data manipulation. In this article, we will discuss some of the most commonly used array methods in JavaScript and provide examples of how to use them.

1) push()

The push() method helps to adds one or more elements to the end of given array. It takes those elements as input arguments.
Here’s an example:

In this example, we have an array of fruits and we use the push method to add the string ‘orange’ to the end of the array.

2) pop()

The pop() method helps to delete the last index element from an array. Also it will return the deleted value. No input arguments required for this method.

Here’s an example:

In this example, we have an array of fruits and we use the pop method to remove the last element (‘orange’) from the array.

3) shift()

The shift() method helps to remove the first element from given array, and return it. It does not have any input arguments input.

Here’s an example:

In this example, we have an array of fruits and we use the shift method to remove the first element (‘apple’) from the array.

4) unshift()

The unshift() method helps to adds one or more elements to the starting index of given array. It takes one or more input arguments, which are the elements to be added to the starting of the given array.

Here’s an example:

In this example, we have an array of fruits and we use the unshift method to add the string ‘apple’ to the beginning of the array.

5) slice()

The slice() method is used to extract a particular section from given array and return a new array. It takes two input arguments: the index of the first element to be included, and the last one.

Here’s an example:

In this example, we have an array of numbers and we use the slice method to extract a section of the array from index 1 to index 4 (exclusive).

6) splice()

The splice() method helps to add and delete elements from given array. It takes three input arguments: the starting index, the number of elements to remove (if any), and the elements to add (if any).

Here’s an example:

In this example, we have an array of fruits and we use the splice method to remove two elements (‘banana’ and ‘orange’) starting from index 1 and add two elements (‘pineapple’ and ‘kiwi’) in their place.

7) concat()

The concat() method is used to merge two or more arrays into a new array It takes one or more array variables as input arguments.

Here’s an example:

In this example, we have two arrays (fruits and vegetables) and we use the concat method to merge them into a new array (fruitsAndVegetables).

8) indexOf()

The indexOf() method is used to find the index of an element in an array It requires one argument, which is the text you want to find..

Here’s an example:

In this example, we have an array of fruits and we use the indexOf method to find the index of the element ‘banana’.

9) forEach()

The forEach() method is used to loop through the elements of an array and perform a function on each element. It takes one argument, which is a function that takes the current element as an argument.

Here’s an example:

In this example, we have an array of numbers and we use the forEach method to loop through each number and log its value multiplied by 2 to the console.

10) map()

The map() method is used to loop through the elements of an array and create a new array with the results of a function performed on each element. It takes one argument, which is a function that takes the current element as an argument.

Here’s an example:

In this example, we have an array of numbers and we use the map method to loop through each number and create a new array with each number multiplied by 2.

Several array methods are commonly used in JavaScript. By mastering these methods, you will be able to manipulate arrays with ease and write more efficient and effective code.

Most Used Array Methods in JavaScript: A Comprehensive Overview

Most Used Array Methods in JavaScript: A Comprehensive Overview

Learn Infinity

Learn Infinity is the most famous Programming & Web Development blog. Our principal is to provide the best online tutorial on web development. We execute the best tutorials for web experts this will help developers, programmers, freelancers and proving free resource you can download or preview the tutorials.

Leave a Reply

Your email address will not be published.

Back to top