Common JavaScript Array Methods

1. pop()

pop() removes the last element from an array and returns that element. This method changes the original array.

let arr = [1, 2, 3, [4, 5, 6]];
console.log(arr.pop()); // [4, 5, 6]
console.log(arr); // [1, 2, 3]

2. push()

push() adds one or more elements to the end of an array and returns the new length of the array. It modifies the original array.

let arr = [1, 2, 3, [4, 5, 6]];
console.log(arr.push([7, 8, 9])); // 5
console.log(arr); // [1, 2, 3, Array(3), Array(3)]

3. unshift()

unshift() adds one or more elements to the beginning of an array and returns the new length. It changes the original array.

let arr = [1, 2, 3, [4, 5, 6]];
console.log(arr.unshift([7, 8, 9])); // 5
console.log(arr); // [Array(3), 1, 2, 3, Array(3)]

4. shift()

shift() removes the first element from an array and returns that element. It modifies the original array.

let arr = [1, 2, 3, [4, 5, 6]];
console.log(arr.shift()); // 1
console.log(arr); // [2, 3, Array(3)]

5. isArray()

Array.isArray() checks whether the passed value is an array. It returns true if it is an array, otherwise false.

let arr = [1, 2, 3, [4, 5, 6]];
console.log(Array.isArray(arr)); // true
console.log(Array.isArray(1)); // false
console.log(Array.isArray({name: 'srd'})); // false

6. map()

map() creates a new array by calling a provided function on every element. It does not change the original array. The callback receives three arguments: currentValue, index, and arr (the array being traversed).

let arr = [1, 2, 3, [4, 5, 6]];
let newArr = arr.map((currentValue, index, arr) => {
    console.log(currentValue, index, arr);
    return currentValue + 1;
});
console.log(arr);
console.log(newArr);
// Output:
// 1 0 (4) [1, 2, 3, Array(3)]
// 2 1 (4) [1, 2, 3, Array(3)]
// 3 2 (4) [1, 2, 3, Array(3)]
// (3) [4, 5, 6] 3 (4) [1, 2, 3, Array(3)]
// Original array: [1, 2, 3, Array(3)]
// New array: [2, 3, 4, "4,5,61"]

Note: The last element is an array, so + 1 converts it to a string via implicit type coercion.

7. filter()

filter() creates a new array with elements that pass a test. It does not mutate the original array. The callback receives currentValue, index, and arr.

let arr = [1, 2, 3, [4, 5, 6]];
let newArr = arr.filter((currentValue, index, arr) => {
    return !Array.isArray(currentValue);
});
console.log(arr); // [1, 2, 3, [4, 5, 6]]
console.log(newArr); // [1, 2, 3]

8. every()

every() checks if all elements pass a test. It returns true if all pass, otherwise false. It does not change the original array. The callback receives currentValue, index, and arr.

let arr = [1, 2, 3, [4, 5, 6]];
let result = arr.every((currentValue, index, arr) => {
    return currentValue > 0;
});
console.log(arr); // [1, 2, 3, [4, 5, 6]]
console.log(result); // false

9. some()

some() checks if at least one element passes a test. It returns true if found, otherwise false. It does not change the original array. The callback receives currentValue, index, and arr.

let arr = [1, 2, 3, [4, 5, 6]];
let result = arr.some((currentValue, index, arr) => {
    return currentValue > 0;
});
console.log(arr); // [1, 2, 3, [4, 5, 6]]
console.log(result); // true

10. splice()

splice() changes the array by removing or replacing existing elements and/or adding new elements. It modifies the oriignal array. Parameters: start (index to begin), deleteCount (number of elements to remove), and optional items to add.

let arr = [1, 2, 3, [4, 5, 6]];

// Remove 2 elements from index 0
let removed = arr.splice(0, 2);
console.log(removed); // [1, 2]
console.log(arr); // [3, [4, 5, 6]]

// Add elements at index 5 (no removal)
arr = [1, 2, 3, [4, 5, 6]];
let added = arr.splice(5, 0, 7, 8, 9);
console.log(added); // []
console.log(arr); // [1, 2, 3, Array(3), 7, 8, 9]

11. slice()

slice() returns a shallow copy of a portion of an array. It does not modify the original array. Parameters: start (inclusive) and end (exclusive). Negative indices count from the end.

let arr = [1, 2, 3, [4, 5, 6]];
let newArr = arr.slice(2, 5);
console.log(newArr); // [3, [4, 5, 6]]
console.log(arr); // [1, 2, 3, [4, 5, 6]]

12. indexOf()

indexOf() returns the first index of a given element, or -1 if not found. It works on both strings and arrays. Parameters: searchElement and optional fromIndex.

let arr = [1, 2, 3, [4, 5, 6], 1];
console.log(arr.indexOf(1)); // 0
console.log(arr.indexOf(1, 2)); // 4
console.log(arr.indexOf(0)); // -1

let str = 'srd';
console.log(str.indexOf('s')); // 0

13. includes()

includes() determines whether an array includes a certain value, returning true or false. Parameters: searchElement and optional fromIndex.

let arr = [1, 2, 3, [4, 5, 6]];
console.log(arr.includes(1)); // true
console.log(arr.includes(1, 1)); // false

14. concat()

concat() merges two or more arrays and returns a new array. It does not change the original arrays.

let arr = [1, 2, 3, [4, 5, 6]];
let arr2 = [7, 8, 9];
console.log(arr.concat(arr2)); // [1, 2, 3, Array(3), 7, 8, 9]

15. join()

join() joins all elements of an array into a string, separated by a specified separator (default comma). It does not change the original array.

let arr = [1, 2, 3, [4, 5, 6]];
console.log(arr.join('---')); // "1---2---3---4,5,6"
console.log(arr.join(1)); // "1121314,5,6"

16. forEach()

forEach() executes a provided function once for each array element. It cannot be broken out of with break or return; only an exception can terminate it. The callback receives value and index.

let arr = [1, 2, 3, [4, 5, 6]];
arr.forEach((value, index) => {
    console.log(value, index);
    throw new Error('Stop');
});
// Output:
// 1 0
// Uncaught Error: Stop

17. sort()

sort() sorts the elements of an array in place and returns the sorted array. It accepts an optional compare function. It modifies the original array.

let arr = [1, 2, 3, 4, [8, 9]];
console.log(arr.sort((a, b) => b - a)); // [4, 3, 2, 1, Array(2)]
console.log(arr); // [4, 3, 2, 1, Array(2)]
console.log(arr.sort((a, b) => a - b)); // [1, 2, 3, 4, Array(2)]

18. reverse()

reverse() reverses the order of the elements in an array in place. The first array element becomes the last, and the last becomes the first.

19. find()

find() returns the value of the first element that passes a test. If no element passes, it returns undefined. It does not change the original array.

let arr = [1, 2, 3, 4, [8, 9], [10, 11]];
console.log(arr.find((item) => {
    return Array.isArray(item);
})); // [8, 9]

20. findIndex()

findIndex() returns the index of the first element that passes a test. If no element passes, it returns -1.

21. fill()

fill() changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.length). It modifies the original array.

let arr = [1, 2, 3, 4, [8, 9], [10, 11]];
let newArr = arr.fill(0);
console.log(arr); // [0, 0, 0, 0, 0, 0]
console.log(newArr); // [0, 0, 0, 0, 0, 0]
console.log(new Array(3).fill(1)); // [1, 1, 1]
console.log(arr.fill(3, 2, 4)); // [0, 0, 3, 3, 0, 0]

Additional iteration methods:

let arr = [1, 2, 3, 4, [8, 9], [10, 11]];

for (let index of arr.keys()) {
    console.log(index); // 0, 1, 2, 3, 4, 5
}

for (let value of arr.values()) {
    console.log(value); // 1, 2, 3, 4, [8, 9], [10, 11]
}

for (let entry of arr.entries()) {
    console.log(entry);
    // [0, 1]
    // [1, 2]
    // [2, 3]
    // [3, 4]
    // [4, Array(2)]
    // [5, Array(2)]
}

22. reduce()

reduce() executes a reducer function on each element, resulting in a single output value. It does not change the original array.

Parameters:

  • callback: function with arguments prev (accumulator), cur (current value), index (optional), arr (optional).
  • initialValue (optional): initial value for the accumulator. If provided, cur starts from the first element; otherwise, prev is the first element and cur starts from the second.
// Syntax:
// arr.reduce(callback, initialValue)

array.reduce((prev, cur, index, arr) => {
    // ...
}, initialValue);

Tags: javascript array methods programming

Posted on Tue, 15 Sep 2026 16:19:01 +0000 by JessePHP