To help you perform common tasks efficiently, JavaScript provides a wide variety of array methods . These methods allow you to add, remove, find, and transform array elements with ease.

javascript_array_methods

1. JavaScript Array length

The length property of an array returns the number of elements in the array. It automatically updates as elements are added or removed.

  
    let a = ["HTML", "CSS", "JS", "React"];
  
  
    console.log(a.length);
  

Output

  
    4
  

In this example

2. JavaScript Array toString() Method

The toString() method converts the given value into the string with each element separated by commas.

  
    let a  = ["HTML", "CSS", "JS", "React"];
  
  
    let s = a.toString();
  
  
    console.log(s);
  

Output

  
    HTML,CSS,JS,React
  

In this example

3. JavaScript Array join() Method

This join() method creates and returns a new string by concatenating all elements of an array. It uses a specified separator between each element in the resulting string.

  
    let a = ["HTML", "CSS", "JS", "React"];
  
  
    console.log(a.join('|'));
  

Output

  
    HTML|CSS|JS|React
  

In this example

4. JavaScript Array delete Operator

The delete operator is used to delete the given value which can be an object, array, or anything.

  
    let emp = {
  
  
    firstName: "Riya",
  
  
    lastName: "Kaur",
  
  
    salary: 40000
  
  
    }
  
  
    console.log(delete emp.salary);
  
  
    console.log(emp);
  

Output

  
    true
{ firstName: 'Riya', lastName: 'Kaur' }
  

In this example

5. JavaScript Array concat() Method

The concat() method is used to concatenate two or more arrays and it gives the merged array.

  
    let a1 = [11, 12, 13];
  
  
    let a2 = [14, 15, 16];
  
  
    let a3 = [17, 18, 19];
  
  
    let newArr = a1.concat(a2, a3);
  
  
    console.log(newArr);
  

Output

  
    [
  11, 12, 13, 14, 15,
  16, 17, 18, 19
]
  

In this example

6. JavaScript Array flat() Method

The flat() method is used to flatten the array i.e. it merges all the given array and reduces all the nesting present in it.

  
    const a1 = [['1', '2'], ['3', '4', '5',['6'], '7']];
  
  
    const a2 = a1.flat(Infinity);
  
  
    console.log(a2);
  

Output

  
    [
  '1', '2', '3',
  '4', '5', '6',
  '7'
]
  

In this example

7. JavaScript Array.push() Method

The push() method is used to add an element at the end of an Array. As arrays in JavaScript are mutable objects, we can easily add or remove elements from the Array.

  
    let a = [10, 20, 30, 40, 50];
  
  
    a.push(60);
  
  
    a.push(70, 80, 90);
  
  
    console.log(a);
  

Output

  
    [
  10, 20, 30, 40, 50,
  60, 70, 80, 90
]
  

8. JavaScript Array.unshift() Method

The unshift() method is used to add elements to the front of an Array.

  
    let a = [20, 30, 40];
  
  
    a.unshift(10, 20);
  
  
    console.log(a);
  

Output

  
    [ 10, 20, 20, 30, 40 ]
  

9. JavaScript Array.pop() Method

The pop() method is used to remove elements from the end of an array.

  
    let a = [20, 30, 40, 50];
  
  
    a.pop();
  
  
    console.log(a);
  

Output

  
    [ 20, 30, 40 ]
  

10. JavaScript Array.shift() Method

The shift() method is used to remove elements from the beginning of an array

  
    let a = [20, 30, 40, 50];
  
  
    a.shift();
  
  
    console.log(a);
  

Output

  
    [ 30, 40, 50 ]
  

11. JavaScript Array.splice() Method

The splice() method is used to Insert and Remove elements in between the Array.

  
    let a = [20, 30, 40, 50];
  
  
    a.splice(1, 3);
  
  
    a.splice(1, 0, 3, 4, 5);
  
  
    console.log(a);
  

Output

  
    [ 20, 3, 4, 5 ]
  

12. JavaScript Array.slice() Method

The slice() method returns a new array containing a portion of the original array, based on the start and end index provided as arguments

  
    const a = [1, 2, 3, 4, 5];
  
  
    const res = a.slice(1, 4);
  
  
    console.log(res);
  
  
    console.log(a)
  

Output

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

In this example

13. JavaScript Array some() Method

The some() method checks whether at least one of the elements of the array satisfies the condition checked by the argument function.

  
    const a = [1, 2, 3, 4, 5];
  
  
    let res = a.some((val) => val > 4);
  
  
    console.log(res);
  

Output

  
    true
  

In this example

14. JavaScript Array map() Method

The map() method creates an array by calling a specific function on each element present in the parent array. It is a non-mutating method.

  
    let a = [4, 9, 16, 25];
  
  
    let sub = a.map(geeks);
  
  
    function geeks() {
  
  
    return a.map(Math.sqrt);
  
  
    }
  
  
    console.log(sub);
  

Output

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

In this example

15. JavaScript Array filter() method

The filter() method in JavaScript creates a new array with all elements that pass the test implemented by the provided function. It does not modify the original array.

  
    let a1 = [1, 2, 3, 4, 5]
  
  
    let a2 = a1.filter((num) => num > 1)
  
  
    console.log(a2)
  

Output

  
    [ 2, 3, 4, 5 ]
  

In this example

16. JavaScript Array reduce() Method

The reduce() method is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator.

  
    let a = [88, 50, 25, 10];
  
  
    let sub = a.reduce(geeks);
  
  
    function geeks(tot, num) {
  
  
    return tot - num;
  
  
    }
  
  
    console.log(sub);
  

Output

  
    3
  

In this example

17. JavaScript Array reverse() method

The reverse() method is used to reverse the order of elements in an array. It modifies the array in place and returns a reference to the same array with the reversed order.

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

Output

  
    [ 5, 4, 3, 2, 1 ]
  

In this example

18. JavaScript Array values() method

The values() method returns a new Array Iterator object that contains the values for each index in the array.

  
    const a = ["Apple", "Banana", "Cherry"];
  
  
    const res = a.values();
  
  
    for (const value of res) {
  
  
    console.log(value);
  
  
    }
  

Output

  
    Apple
Banana
Cherry
  

In this example