JavaScript array methods are built-in functions that add, remove, search, change and combine the values in an array. This page lists the important ones, shows the real output of each and marks which methods change the original array.
Every example was run in Node.js 22. Paste any block into your browser console or a .js file to try it.
JavaScript array methods list
Use this JavaScript array methods list to pick by task.
| Task | Methods |
|---|---|
| Add or remove at the ends | push, pop, unshift, shift |
| Add or remove in the middle | splice, toSpliced |
| Copy or join | slice, concat, join |
| Go through each value | forEach, map, filter, reduce |
| Search | includes, indexOf, find, findIndex, findLast |
| Test | some, every |
| Order | sort, reverse, toSorted, toReversed |
| Flatten | flat, flatMap |
| Read or replace one position | at, with |
| Create | Array.from, Array.of, fill |
Does it change the original?
This is the most useful fact to remember.
| Changes the array | Returns a new array or value |
|---|---|
push, pop, shift, unshift |
slice, concat, map, filter |
splice |
toSpliced |
sort |
toSorted |
reverse |
toReversed |
fill, copyWithin |
with, flat, flatMap, reduce, join |
Methods to add and remove items
const langs = ["Java", "Python"];
console.log(langs.push("SQL")); // 3
console.log(langs.unshift("C")); // 4
console.log(langs.pop()); // SQL
console.log(langs.shift()); // C
console.log(langs); // [ 'Java', 'Python' ]
push and unshift return the new length. pop and shift return the removed value.
splice(start, deleteCount, ...items) works anywhere in the array:
const nums = [10, 20, 30, 40];
const removed = nums.splice(1, 2, 99);
console.log(removed); // [ 20, 30 ]
console.log(nums); // [ 10, 99, 40 ]
It started at index 1, removed two values and put 99 in their place.
Common mistake: Mixing up
sliceandsplice.slice(1, 3)copies from index 1 up to, but not including, index 3 and leaves the array alone.splice(1, 3)removes three values from the array itself.
const letters = ["a", "b", "c", "d"];
console.log(letters.slice(1, 3)); // [ 'b', 'c' ]
console.log(letters.slice(-2)); // [ 'c', 'd' ]
console.log(letters.length); // 4
console.log(letters.at(-1)); // d
JavaScript array methods map, filter, reduce
The three JavaScript array methods map, filter and reduce take a function and leave the original array as it was.
mapchanges every value. The result has the same length.filterkeeps values that pass a test. The result may be shorter.reducecombines all values into one result.
const cart = [
{ name: "Book", price: 300, qty: 2 },
{ name: "Pen", price: 20, qty: 5 },
{ name: "Bag", price: 900, qty: 1 },
];
const names = cart.map((item) => item.name);
console.log(names); // [ 'Book', 'Pen', 'Bag' ]
const costly = cart.filter((item) => item.price >= 300);
console.log(costly.length); // 2
const total = cart.reduce((sum, item) => sum + item.price * item.qty, 0);
console.log(total); // 1600
Check the total by hand: 600 + 100 + 900 = 1600.
The 0 at the end of reduce is the starting value. Always give it. Without it, reduce on an empty array throws a TypeError.
You can also use reduce to count:
const votes = ["yes", "no", "yes", "yes"];
const count = votes.reduce((acc, v) => {
acc[v] = (acc[v] ?? 0) + 1;
return acc;
}, {});
console.log(count); // { yes: 3, no: 1 }
Interview note: You may be asked for the difference between
mapandforEach.mapreturns a new array.forEachreturnsundefinedand is used for side effects such as printing. You cannot stop either one withbreak, so usefor...ofwhen you need to stop early.
Methods to search and test
const users = [
{ id: 1, name: "Asha", active: true },
{ id: 2, name: "Ravi", active: false },
{ id: 3, name: "Meena", active: true },
];
console.log(users.find((u) => u.id === 2).name); // Ravi
console.log(users.findIndex((u) => u.id === 9)); // -1
console.log(users.findLast((u) => u.active).name); // Meena
console.log(users.some((u) => u.active)); // true
console.log(users.every((u) => u.active)); // false
find gives undefined when nothing matches, so check the result before reading a property from it.
For simple values, use includes or indexOf:
const marks = [45, NaN, 80];
console.log(marks.includes(80)); // true
console.log(marks.indexOf(80)); // 2
console.log(marks.includes(NaN)); // true
console.log(marks.indexOf(NaN)); // -1
includes can find NaN, but indexOf cannot.
Methods to sort and reverse
sort without a function compares values as text. This surprises almost everyone once.
const n = [10, 9, 1, 100];
console.log([...n].sort()); // [ 1, 10, 100, 9 ]
console.log([...n].sort((a, b) => a - b)); // [ 1, 9, 10, 100 ]
console.log(n.toSorted((a, b) => b - a)); // [ 100, 10, 9, 1 ]
console.log(n); // [ 10, 9, 1, 100 ]
As text, "100" comes before "9". Pass (a, b) => a - b for numbers going up. For names, use localeCompare:
const people = [{ n: "Ravi" }, { n: "Asha" }];
people.sort((a, b) => a.n.localeCompare(b.n));
console.log(people[0].n); // Asha
Pro tip: In React, never call
sortorreversedirectly on state, because both change the array in place. UsetoSortedandtoReversed, or copy first with[...list].
Methods to flatten, join and create arrays
console.log([1, [2, [3]]].flat()); // [ 1, 2, [ 3 ] ]
console.log([1, [2, [3]]].flat(Infinity)); // [ 1, 2, 3 ]
console.log(["a b", "c"].flatMap((s) => s.split(" "))); // [ 'a', 'b', 'c' ]
console.log(["2026", "12", "03"].join("-")); // 2026-12-03
console.log([1, 2].concat([3], 4)); // [ 1, 2, 3, 4 ]
console.log(Array.from("abc")); // [ 'a', 'b', 'c' ]
console.log(Array.from({ length: 3 }, (_, i) => i * i)); // [ 0, 1, 4 ]
console.log([1, 2, 3].with(1, 99)); // [ 1, 99, 3 ]
console.log([...new Set([1, 1, 2])]); // [ 1, 2 ]
The last line is not an array method, but it is the shortest way to remove duplicates.
Be careful with fill and objects. Every position gets the same object:
const grid = new Array(2).fill([]);
grid[0].push("x");
console.log(grid); // [ [ 'x' ], [ 'x' ] ]
const safe = Array.from({ length: 2 }, () => []);
safe[0].push("x");
console.log(safe); // [ [ 'x' ], [] ]
Chaining methods: a full example
Methods that return an array can be joined in a chain. Read it from top to bottom.
const results = [
{ name: "Asha", marks: 82 },
{ name: "Ravi", marks: 35 },
{ name: "Meena", marks: 91 },
];
const line = results
.filter((r) => r.marks >= 40)
.toSorted((a, b) => b.marks - a.marks)
.map((r) => `${r.name} (${r.marks})`)
.join(", ");
console.log(line); // Meena (91), Asha (82)
It keeps students who passed, sorts them by marks, makes text for each and joins the text.
Each step walks through the array once, which is fine for normal lists. For very large data, one for...of loop uses less memory because it builds no middle arrays.
JavaScript array methods cheat sheet and reference
This JavaScript array methods cheat sheet shows what each call gives back. For a full JavaScript array methods reference with browser support, use the MDN Array page.
| Call | Gives back |
|---|---|
a.push(x) |
New length |
a.pop() |
Removed last value |
a.slice(1, 3) |
Copy of part |
a.splice(1, 2) |
Array of removed values |
a.map(fn) |
New array, same length |
a.filter(fn) |
New array of matches |
a.reduce(fn, start) |
One final value |
a.find(fn) |
First match or undefined |
a.findIndex(fn) |
Index or -1 |
a.some(fn) / a.every(fn) |
true or false |
a.includes(x) |
true or false |
a.sort(fn) |
The same array, sorted |
a.toSorted(fn) |
New sorted array |
a.flat(depth) |
New flatter array |
toSorted, toReversed, toSpliced and with came with ES2023. Node.js 20 and later have them. In an older setup, copy with [...a] first and then use the older method.
Practice JavaScript array methods with examples
You now have JavaScript array methods with examples for daily work. Solve these without a loop:
- From
[3, 8, 5, 12], get the squares of the even numbers. - Find the longest word in a list.
- Group words by their first letter.
console.log([3, 8, 5, 12].filter((x) => x % 2 === 0).map((x) => x * x));
// [ 64, 144 ]
const words = ["sun", "planet", "star", "sky"];
console.log(words.reduce((a, b) => (b.length > a.length ? b : a)));
// planet
const groups = words.reduce((acc, w) => {
(acc[w[0]] ??= []).push(w);
return acc;
}, {});
console.log(groups);
// { s: [ 'sun', 'star', 'sky' ], p: [ 'planet' ] }
For more tasks, try JavaScript practice problems and revise syntax with the JavaScript cheat sheet. Then prepare with ES6 interview questions. You will use map and filter every day in the React JS tutorial, which is also part of the MERN stack course.
Frequently Asked Questions
Which JavaScript array methods change the original array?
What is the difference between map and forEach?
What is the difference between slice and splice?
Why does sort give a wrong order for numbers?
How do I remove duplicates from an array?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — Check the Java Full Stack training details

