Array Methods

Beyond adding and removing at the ends, arrays have methods for searching, slicing out sections, inserting in the middle, combining arrays and flattening nested ones. A key skill is knowing which methods change the original array and which return a new one.

Searching

JS search.js
const pets = ["cat", "dog", "fish", "dog"];

console.log(pets.includes("fish"));
console.log(pets.indexOf("dog"));
console.log(pets.lastIndexOf("dog"));
console.log(pets.indexOf("bird"));
Output
true
1
3
-1

includes answers "is it there?"; indexOf tells you where (or -1). Both compare with strict equality, so they work for primitives but can't find an object by its contents — use find/findIndex for that (next lesson).

slice: copy a section

slice(start, end) returns a new array with items from start up to, but not including, end. The original is untouched:

JS slice.js
const letters = ["a", "b", "c", "d", "e"];

console.log(letters.slice(1, 3));
console.log(letters.slice(2));
console.log(letters.slice(-2));
console.log(letters.slice());
console.log(letters);
Output
[ 'b', 'c' ]
[ 'c', 'd', 'e' ]
[ 'd', 'e' ]
[ 'a', 'b', 'c', 'd', 'e' ]
[ 'a', 'b', 'c', 'd', 'e' ]

splice: insert and remove anywhere

splice(start, deleteCount, ...itemsToInsert) changes the array in place and returns the removed items:

JS splice.js
const months = ["Jan", "Mar", "Apr", "Jun"];

months.splice(1, 0, "Feb");
console.log(months);

const removed = months.splice(3, 1);
console.log(removed, months);

months.splice(3, 1, "May", "Jun");
console.log(months);
Output
[ 'Jan', 'Feb', 'Mar', 'Apr', 'Jun' ]
[ 'Apr' ] [ 'Jan', 'Feb', 'Mar', 'Jun' ]
[ 'Jan', 'Feb', 'Mar', 'May', 'Jun' ]

The names are easy to mix up: slice copies, splice modifies.

Non-mutating alternatives

Since 2023, arrays have "change by copy" versions of the mutating methods. They return a new array and leave the original alone — handy in frameworks like React, where state must not be modified directly:

JS by-copy.js
const nums = [3, 1, 2];

console.log(nums.toSorted());
console.log(nums.toReversed());
console.log(nums.toSpliced(1, 1));
console.log(nums.with(0, 99));
console.log(nums);
Output
[ 1, 2, 3 ]
[ 2, 1, 3 ]
[ 3, 2 ]
[ 99, 1, 2 ]
[ 3, 1, 2 ]

Combining and joining

JS combine.js
const a = [1, 2];
const b = [3, 4];

console.log(a.concat(b));
console.log([...a, ...b, 5]);
console.log(["2026", "10", "06"].join("-"));
console.log([1, 2, 3].join(""));
Output
[ 1, 2, 3, 4 ]
[ 1, 2, 3, 4, 5 ]
2026-10-06
123

Spread syntax is the most flexible way to combine arrays. join turns an array into a string with any separator (comma by default).

Reversing and filling

JS reverse-fill.js
const order = [1, 2, 3];
order.reverse();
console.log(order);

const seats = new Array(5).fill("free");
seats.fill("taken", 1, 3);
console.log(seats);
Output
[ 3, 2, 1 ]
[ 'free', 'taken', 'taken', 'free', 'free' ]

Flattening nested arrays

JS flat.js
const nested = [1, [2, 3], [4, [5, 6]]];

console.log(nested.flat());
console.log(nested.flat(Infinity));

const sentences = ["hello world", "good morning"];
console.log(sentences.flatMap(s => s.split(" ")));
Output
[ 1, 2, 3, 4, [ 5, 6 ] ]
[ 1, 2, 3, 4, 5, 6 ]
[ 'hello', 'world', 'good', 'morning' ]

flat() flattens one level by default; pass a depth or Infinity for more. flatMap maps each item to an array and flattens the result one level.

Mutating vs non-mutating

Changes the originalReturns a new array / value
push, pop, shift, unshiftslice, concat, spread [...]
splicetoSpliced, with
sort, reversetoSorted, toReversed
fill, copyWithinmap, filter, flat, flatMap, join

Try it yourself

Try it yourself
Console output