JavaScript Array Methods
Arrays come with a rich toolbox of methods. This chapter covers the ones for adding, removing, combining and slicing elements. An important distinction runs through all of them: some methods mutate (change) the original array, others return a new array and leave the original alone.
Adding and removing at the ends
| Method | Where | Mutates? | Returns |
|---|---|---|---|
push(x, …) |
end | ✅ | new length |
pop() |
end | ✅ | removed element |
unshift(x, …) |
start | ✅ | new length |
shift() |
start | ✅ | removed element |
const queue = ["Ana", "Ben"];
queue.push("Cleo"); // join at the back
queue.unshift("Dev"); // jump to the front
console.log(queue);
const served = queue.shift(); // serve the first person
const left = queue.pop(); // the last one leaves
console.log(served, left, queue);[ 'Dev', 'Ana', 'Ben', 'Cleo' ]
Dev Cleo [ 'Ana', 'Ben' ]
splice — the Swiss army knife
splice(start, deleteCount, ...itemsToInsert) removes and/or inserts elements anywhere, in place, and returns the removed elements:
const months = ["Jan", "Feb", "Apr", "May", "Jun"];
months.splice(2, 0, "Mar"); // insert at index 2, remove nothing
console.log(months);
const removed = months.splice(4, 2); // remove 2 elements from index 4
console.log(removed, months);
months.splice(1, 1, "FEB"); // replace one element
console.log(months);[ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun' ]
[ 'May', 'Jun' ] [ 'Jan', 'Feb', 'Mar', 'Apr' ]
[ 'Jan', 'FEB', 'Mar', 'Apr' ]
slice — copy a part
slice(start, end) returns a new array with the elements from start up to (not including) end. The original is unchanged. With no arguments it copies the whole array:
const letters = ["a", "b", "c", "d", "e"];
console.log(letters.slice(1, 3));
console.log(letters.slice(-2));
console.log(letters.slice());
console.log(letters); // untouched[ 'b', 'c' ]
[ 'd', 'e' ]
[ 'a', 'b', 'c', 'd', 'e' ]
[ 'a', 'b', 'c', 'd', 'e' ]
Tip:
sliceandsplicediffer by one letter but behave very differently: slice is a safe look, splice performs surgery.
Combining and flattening
const a = [1, 2];
const b = [3, 4];
console.log(a.concat(b, [5]));
console.log([...a, ...b, 5]); // spread does the same
console.log([1, [2, [3, [4]]]].flat());
console.log([1, [2, [3, [4]]]].flat(Infinity));
console.log(["hi there", "bye"].flatMap((s) => s.split(" ")));[ 1, 2, 3, 4, 5 ]
[ 1, 2, 3, 4, 5 ]
[ 1, 2, [ 3, [ 4 ] ] ]
[ 1, 2, 3, 4 ]
[ 'hi', 'there', 'bye' ]
Converting to strings
const parts = ["2025", "03", "14"];
console.log(parts.join("-"));
console.log(parts.join(""));
console.log(parts.toString());
console.log(String([1, [2, 3]]));2025-03-14
20250314
2025,03,14
1,2,3
Non-mutating versions (ES2023)
Newer methods return a changed copy instead of modifying the array: toSpliced, toSorted, toReversed and with(index, value):
const original = [3, 1, 2];
console.log(original.toReversed());
console.log(original.toSorted());
console.log(original.with(0, 99));
console.log(original.toSpliced(1, 1));
console.log(original); // still [3, 1, 2][ 2, 1, 3 ]
[ 1, 2, 3 ]
[ 99, 1, 2 ]
[ 3, 2 ]
[ 3, 1, 2 ]
These are handy in frameworks like React, where you should never mutate state directly.
Filling and length
fill(value, start, end) overwrites elements with a value. Setting length to a smaller number truncates the array:
const seats = new Array(5).fill("free"); // ['free', 'free', 'free', 'free', 'free']
seats.length = 2; // ['free', 'free']Quick reference
| Mutates the array | Returns a new array/value |
|---|---|
push pop shift unshift |
slice concat flat flatMap |
splice fill reverse sort |
join toString |
copyWithin |
toSpliced toSorted toReversed with |
Test yourself
Which method removes the first element of an array?
- pop()
- shift()
- unshift()
- slice(1)
What does [1, 2, 3, 4].slice(1, 3) return?
- [1, 2, 3]
- [2, 3]
- [2, 3, 4]
- [1, 4]
Which of these does NOT change the original array?
- splice
- push
- toSorted
- reverse