Important Array Methods of JavaScript (original) (raw)

JavaScript arrays are powerful tools for managing collections of data. They come with a wide range of built-in methods that allow developers to manipulate, transform, and interact with array elements.

Some of the most important array methods in JavaScript are

Table of Content

**1. JavaScript push() Method

The push() method adds one or more elements to the end of an array and returns the new length.

JavaScript `

let a = ['Hello','GFG','JS']; a.push('React'); console.log(a);

`

Output

[ 'Hello', 'GFG', 'JS', 'React' ]

**In this example

**2. JavaScript pop() Method

The pop() method removes the last element from an array and returns it.

JavaScript `

let a = ["Hello", "GFG", "JS"]; let ele = a.pop(); console.log(ele); console.log(a);

`

Output

JS [ 'Hello', 'GFG' ]

**In this example

**3. JavaScript shift() Method

The shift() method removes the first element from an array and returns it.

JavaScript `

let a = ["Hello", "GFG", "JS"]; let ele = a.shift(); console.log(ele); console.log(a);

`

Output

Hello [ 'GFG', 'JS' ]

**In this example

**4. unshift() Method

The unshift() method is used to add the elements in the array from the front side.

JavaScript `

let a = ["Hello", "GFG", "JS"]; a.unshift("React"); console.log(a);

`

Output

[ 'React', 'Hello', 'GFG', 'JS' ]

**In this example

**5. JavaScript indexOf() Method

The indexOf() method is used to find the index of a particular element in an array.

JavaScript `

let a = ["Hello", "GFG", "JS"]; console.log(a.indexOf('GFG'));

`

**In this example

**6. JavaScript includes() Method

The includes() method is used to check whether the array contains the specified element or not.

JavaScript `

let a = ["Hello", "GFG", "JS"]; console.log(a.includes("GFG")); console.log(a.includes("React"));

`

**In this example

**7. JavaScript concat() Method

The concat() method is used to concat or basically join two different arrays from end to end.

JavaScript `

let a1 = ["Hello","GFG", "Js"]; let a2 = ["React"]; let new = a1.concat(a2);

console.log(a1); console.log(a2); console.log(new);

`

**Output:

[ 'Hello', 'GeeksforGeeks', 'JavaScript' ]
[ 'React' ]
[ 'Hello', 'GeeksforGeeks', 'JavaScript', 'React' ]

**In this example

**8. JavaScript forEach() Method

The forEach() works as a loop over an array where for every element the function just runs once only.

JavaScript `

let a = ["Hello", "GFG", "JS"]; a.forEach(function (element) { console.log(element) });

`

Output

Hello GeeksforGeeks JavaScript

**In this example

**9. JavaScript sort() Method

The sort() method sorts the elements of an array in alphabetical order in ascending order.

JavaScript `

let a = ["Hello", "GFG", "JS"]; console.log(a); a.sort(); console.log(a);

`

Output

[ 'Hello', 'GeeksforGeeks', 'JavaScript' ] [ 'GeeksforGeeks', 'Hello', 'JavaScript' ]

**In this example

**10. JavaScript map() method

The map() method iterates over an array, transforms the array according to user-specified conditions and returns a new array.

JavaScript `

let a1 = [1, 2, 3, 4, 5]; console.log(a1);

let a2 = a1.map(function (elem) { return elem * 5; }); console.log(a2);

`

Output

[ 1, 2, 3, 4, 5 ] [ 5, 10, 15, 20, 25 ]

**In this example

**11. JavaScript reduce() Method

The reduce() method uses a reducer function that reduces the results into a single output.

JavaScript `

let a1 = [1, 2, 3, 4, 5]; console.log(a1);

let sum = a1.reduce( function (acc, elem) { return acc + elem; });

console.log(sum);

`

Output

[ 1, 2, 3, 4, 5 ] 15

**In this example

**12. JavaScript filter() Method

The filter() method is used to filter out the contents, as per the user-specified condition, in the form of a new array.

JavaScript `

let a1 = [1, 2, 3, 4, 5]; console.log(a1);

let a2 = a1.filter(function (elem) { return elem % 2 === 0; }); console.log(a2);

`

Output

[ 1, 2, 3, 4, 5 ] [ 2, 4 ]

**In this example

**13. JavaScript find() & findIndex() Method

The find() method finds out the first value which passes the user-specified conditions and findIndex() method finds out the first index value which passes the user-specified conditions.

JavaScript `

let a = [1, 2, 3, 4, 5]; let find = a.find(function (elem) { return elem > 4 }); console.log(find);

let val = a.findIndex(function (elem) { return elem >= 4 }); console.log(val);

`

**In this example

**14. JavaScript slice() & splice() Method

The slice() selects the specified number of elements without affecting the original array elements whereas splice() removes the selected elements from the original array itself.

JavaScript `

let a = [1, 2, 3, 4, 5]; let slice = a.slice(0, 2); console.log("Slice Array: " + slice); console.log("Original Array: " + a); let splice = a.splice(0, 2); console.log("Slice Array: " + splice); console.log("Original Array: " + a);

`

Output

Slice Array: 1,2 Original Array: 1,2,3,4,5 Slice Array: 1,2 Original Array: 3,4,5

**In this example

**15. JavaScript some() and every() Method

The some() method checks the user-specified conditions on some elements of an array (i.e. it checks for a few elements only), whereas every() method checks the user-specified conditions on every element of an array then returns the results in true or false.

JavaScript `

let a = [1, 2, 3, 4, 5]; let n1 = a.every( function (elem) { return elem > 0 }); let n2 = a.some( function (elem) { return elem < 0 }); console.log(n1); console.log(n2);

`

**In this example

16. Javascript forEach() method

The forEach() method in JavaScript is used to execute a provided function once for each element in an array.

JavaScript `

let n = [1, 2, 3, 4, 5]; n.forEach(function(elem, index) { console.log(elem); });

`

**In this example

17. JavaScript Array reverse() Method

The reverse() method is used to reverse the order of the elements in an array.

JavaScript `

let a = [1, 2, 3, 4, 5]; a.reverse(); console.log(a);

`

**In this example

18. Javascript Array every() method

The every() method is used to check if all elements in the array pass the test implemented by the provided function. It returns a boolean value true if all elements satisfy the test otherwise returns false.

JavaScript `

const a = [2, 4, 6]; const isEven = (num) => num % 2 === 0; const allEven = a.every(isEven); console.log(allEven);

`