JavaScript array methods add, remove, slice, combine, and format list items cleanly. Knowing which methods modify the original array in place and which return a brand-new array prevents hard-to-find state bugs in React and Node.js.
Choosing the right method depends on whether you want to change the existing array or return a new copy. Keep this reference handy when managing application state:
| Method | Mutates Original? | Purpose |
|---|---|---|
push() / pop() |
Yes | Add or remove from the end |
unshift() / shift() |
Yes | Add or remove from the start |
splice(i, count) |
Yes | Insert or delete items at index i |
slice(start, end) |
No | Copy a sub-range into a new array |
toSpliced(i, count) |
No (ES2023) | Immutable replacement for splice() |
join(sep) |
No | Join all items into one string |
Before ES2023, developers cloned arrays with [...arr] before calling splice() or reverse(). Modern toSpliced(), toReversed(), and with() perform those updates immutably in a single step.
In modern frontend apps, prefer non-mutating methods like slice(), toSpliced(), and spread [...arr] so state updates stay predictable.
const tasks = ["Design", "Build", "Test", "Deploy"];
// Non-mutating slice and join
const middle = tasks.slice(1, 3); // ["Build", "Test"]
console.log("Middle tasks:", middle.join(" -> "));
// ES2023 non-mutating splice (falls back cleanly in modern runtimes)
const updated = tasks.toSpliced(2, 1, "Review", "Test");
console.log("Original intact:", tasks);
console.log("Updated copy:", updated);
// Stack behavior at the end
const stack = ["step1", "step2"];
stack.push("step3");
const last = stack.pop();
console.log("Popped:", last, "Remaining:", stack);
slice() and splice(): slice() copies a range without touching the source array, whereas splice() deletes or inserts directly inside the source array.push() to return the array: arr.push("x") returns the new length number, not the modified array.shift() inside huge loops: Removing from the front of a huge array shifts every remaining element's index; use pop() or index pointers for queues.Call nested.flat() for one level deep, or nested.flat(Infinity) to flatten arbitrary nesting into a single flat array.
Use ES2023 arr.with(index, newValue) or .map(), both of which return a new array copy and leave the original untouched.
Use [...firstArray, ...secondArray] or firstArray.concat(secondArray).
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.