Learning objective: Add and remove elements with push, pop, unshift, shift and splice, copy parts with slice and concat, and tell mutating methods from non-mutating ones.
push(x) adds to the end and pop() removes the last element and returns it. unshift(x) adds to the start and shift() removes the first element.
splice(start, deleteCount, ...items) removes or inserts elements anywhere: list.splice(1, 1) removes one element at index 1.
These five methods mutate (change) the original array. slice(start, end) and concat() do not: they return a new array and leave the original as it was.
Read a method's return value: push returns the new length, pop and shift return the removed element, splice returns an array of removed elements.
Mutation is visible through every reference to the same array. Shared state mutated in one function causes bugs elsewhere; return new arrays when data is shared, and mutate only local arrays you own.
ES2023 added non-mutating counterparts: toSpliced, toSorted, toReversed and with(index, value). Spread ([...a, x]) is the common way to append without mutating.
shift and unshift move every element, so they are O(n); push and pop are amortised O(1). For large queues, track a start index or use a different structure.
slice() with no arguments makes a shallow copy: the new array holds the same object references as the original.
05Definitions
push / pop
Add to or remove from the end of an array.
unshift / shift
Add to or remove from the start of an array.
splice
Removes or inserts elements at any index, changing the array.
slice
Returns a copy of part of an array without changing it.
Mutation
Changing an existing value in place instead of creating a new one.
const queue = ["T1", "T2"];
// TODO: T3 and T4 join the end; then serve (remove) the first two tickets
console.log("Served:", served.join(", "));
console.log("Waiting:", queue.join(", "));
One page of results
Use slice to read a page without changing the data.
A cart adds items with push, removes a chosen line with splice, and shows a preview of the first three items with slice so the cart itself never changes while it is displayed.
2. Which method does NOT change the original array?
push
splice
slice
shift
3. What is printed?
console.log(["a", "b", "c", "d"].slice(1, 3));
["b", "c"]
["a", "b"]
["b"]
["a"]
4. Why is the wrong item removed?
const i = list.indexOf("Rug");
list.splice(i);
splice needs a count
indexOf is used correctly but splice(i) removes to the end
pop should be used
Arrays cannot remove from the middle
5. What does shift return?
The new length
The removed first element
A new array
Nothing
6. A function receives a shared list and must add an item without affecting other code. Which is safer?
list.push(item); return list;
return [...list, item];
Both are the same
list.unshift(item)
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Remove the right item: Only Drive removed
Manage the queue: Prints both lines; Uses shift
One page of results: Prints three pages; Original unchanged; Page 4 is empty
ex-027-01: output matches the documented expected output
ex-027-02: output matches the documented expected output
ex-027-03: output matches the documented expected output
ex-027-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-02-core-development/module-06-arrays-and-collections/lesson-027-adding-and-removing-elements/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
15Summary and next steps
push/pop work at the end, unshift/shift at the start.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationarray mutation methods
Context: I am learning array mutation methods in JavaScript.
Task: Explain push, pop, shift, unshift, splice and slice with a cart example, marking which ones change the array.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionarray mutation methods drill
Context: I have practised array mutation methods.
Task: Give me eight short programs using these methods and ask what each prints.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingarray mutation methods bug
Context: My splice call removes more items than I wanted.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationarray mutation methods practice
Context: I finished the array mutation methods exercises.
Task: Create three exercises using push, splice and slice with exact expected output.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewarray mutation methods review
Context: Here is my code that uses array mutation methods.
Task: Review where my code mutates shared arrays and suggest non-mutating alternatives.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.