Javascript
JS Arrays
Important Array Methods of Javascript

Important Array Methods of JavaScript

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.

Table of Content

  1. JavaScript push() Method
  2. JavaScript pop() Method
  3. JavaScript shift() Method
  4. unshift() Method
  5. JavaScript indexOf() Method
  6. JavaScript includes() Method
  7. JavaScript concat() Method
  8. JavaScript forEach() Method
  9. JavaScript sort() Method
  10. JavaScript map() Method
  11. JavaScript reduce() Method
  12. JavaScript filter() Method
  13. JavaScript find() & findIndex() Method
  14. JavaScript slice() & splice() Method
  15. JavaScript some() and every() Method
  16. JavaScript Array reverse() Method

JavaScript push() Method

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

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

Output:

[ 'Hello', 'Coding75', 'JS', 'React' ]

JavaScript pop() Method

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

<<<<<<< HEAD
let a = ["Hello", "Coding75", "JS"];
=======
let a = ["Hello", "TOC", "JS"];
>>>>>>> 6c9caef (content Overview Complete)
let ele = a.pop();
console.log(ele);
console.log(a);

Output:

JS
<<<<<<< HEAD
[ 'Hello', 'Coding75' ]
=======
[ 'Hello', 'TOC' ]
>>>>>>> 6c9caef (content Overview Complete)

JavaScript shift() Method

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

<<<<<<< HEAD
let a = ["Hello", "Coding75", "JS"];
=======
let a = ["Hello", "TOC", "JS"];
>>>>>>> 6c9caef (content Overview Complete)
let ele = a.shift();
console.log(ele);
console.log(a);

Output:

Hello
<<<<<<< HEAD
[ 'Coding75', 'JS' ]
=======
[ 'TOC', 'JS' ]
>>>>>>> 6c9caef (content Overview Complete)

unshift() Method

The unshift() method adds elements to the beginning of an array.

<<<<<<< HEAD
let a = ["Hello", "Coding75", "JS"];
=======
let a = ["Hello", "TOC", "JS"];
>>>>>>> 6c9caef (content Overview Complete)
a.unshift("React");
console.log(a);

Output:

<<<<<<< HEAD
[ 'React', 'Hello', 'Coding75', 'JS' ]
=======
[ 'React', 'Hello', 'TOC', 'JS' ]
>>>>>>> 6c9caef (content Overview Complete)

JavaScript indexOf() Method

The indexOf() method returns the first index of a specified element in an array.

<<<<<<< HEAD
let a = ["Hello", "Coding75", "JS"];
console.log(a.indexOf('Coding75'));
=======
let a = ["Hello", "TOC", "JS"];
console.log(a.indexOf('TOC'));
>>>>>>> 6c9caef (content Overview Complete)

Output:

1

JavaScript includes() Method

The includes() method checks whether an array contains a specific element.

<<<<<<< HEAD
let a = ["Hello", "Coding75", "JS"];
console.log(a.includes("Coding75"));
=======
let a = ["Hello", "TOC", "JS"];
console.log(a.includes("TOC"));
>>>>>>> 6c9caef (content Overview Complete)
console.log(a.includes("React"));

Output:

true
false

JavaScript concat() Method

The concat() method merges two arrays into a new array.

<<<<<<< HEAD
let a1 = ["Hello", "Coding75", "Js"];
=======
let a1 = ["Hello", "TOC", "Js"];
>>>>>>> 6c9caef (content Overview Complete)
let a2 = ["React"];
let newArr = a1.concat(a2);
console.log(newArr);

Output:

<<<<<<< HEAD
[ 'Hello', 'Coding75', 'Js', 'React' ]
=======
[ 'Hello', 'TOC', 'Js', 'React' ]
>>>>>>> 6c9caef (content Overview Complete)

JavaScript forEach() Method

The forEach() method executes a function once per array element.

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

Output:

Hello
TOC
JS

JavaScript sort() Method

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

<<<<<<< HEAD
let a = ["Hello", "Coding75", "JS"];
=======
let a = ["Hello", "TOC", "JS"];
>>>>>>> 6c9caef (content Overview Complete)
a.sort();
console.log(a);

Output:

<<<<<<< HEAD
[ 'Coding75', 'Hello', 'JS' ]
=======
[ 'TOC', 'Hello', 'JS' ]
>>>>>>> 6c9caef (content Overview Complete)

JavaScript map() Method

The map() method creates a new array by applying a function to each element.

let a1 = [1, 2, 3, 4, 5];
let a2 = a1.map(elem => elem * 5);
console.log(a2);

Output:

[ 5, 10, 15, 20, 25 ]

JavaScript reduce() Method

The reduce() method applies a function to reduce the array to a single value.

let a1 = [1, 2, 3, 4, 5];
let sum = a1.reduce((acc, elem) => acc + elem, 0);
console.log(sum);

Output:

15

JavaScript filter() Method

The filter() method filters elements based on a condition.

let a1 = [1, 2, 3, 4, 5];
let a2 = a1.filter(elem => elem % 2 === 0);
console.log(a2);

Output:

[ 2, 4 ]

This document contains detailed explanations of various JavaScript array methods with code examples and outputs.