01 / 21

JavaScript
Array Methods

Master the essential methods for adding, removing, and extracting elements from arrays: push, pop, shift, unshift, splice, and slice.

JavaScript

What Is an Array?

An array is an ordered list of values stored in a single variable. Each value sits at a numbered position called an index, starting at 0.

"a"0
"b"1
"c"2
"d"3

The Two Ends of an Array

10 20 30 40 FRONT index 0 END last index 0 1 2 3
shift / unshift
Operate on the front (index 0)
push / pop
Operate on the end (last index)

Mutate vs. Copy

Some methods change the original array in place (mutate). Others return a new array without touching the original (copy). Knowing which is which prevents bugs.

Mutating
push, pop, shift, unshift, splice
Non-Mutating
slice returns a brand-new array

push and pop

1 2 3 4 .push(4) adds to end 1 2 3 .pop() removes from end returns 3
// .push() adds an element to the end.
let arr = [1, 2, 3];
arr.push(4);     // arr is now [1, 2, 3, 4]

// .pop() removes an element from the end.
let arr2 = [1, 2, 3];
let removed = arr2.pop();
// removed is 3, arr2 is [1, 2]

unshift and shift

0 1 2 3 .unshift(0) adds to front 1 2 3 .shift() removes front returns 1
// .unshift() adds an element to the front.
let arr = [1, 2, 3];
arr.unshift(0);  // arr is now [0, 1, 2, 3]

// .shift() removes an element from the front.
let arr2 = [1, 2, 3];
let removed = arr2.shift();
// removed is 1, arr2 is [2, 3]

splice: The Swiss Army Knife

splice(start, deleteCount, ...items) can remove, insert, or replace elements at any position. It mutates the original array and returns the removed elements.

// Removing elements
let arr = ["a", "b", "c", "d", "e"];
arr.splice(1, 2);
// removed: ["b", "c"]
// arr is now ["a", "d", "e"]

splice: Insert and Replace

// Inserting without removing
let arr = [1, 2, 5];
arr.splice(2, 0, 3, 4);
// arr is now [1, 2, 3, 4, 5]

// Replacing elements
let colors = ["red", "green", "blue"];
colors.splice(1, 1, "yellow");
// colors is now ["red", "yellow", "blue"]
Insert
Set deleteCount to 0, then pass new items
Replace
Delete some elements, then insert new ones in their place

slice: Copy Without Mutating

slice(start, end) returns a new array containing elements from start up to (but not including) end. The original stays unchanged.

10 20 30 40 50 0 1 2 3 4 slice(1, 4) result: 20 30 40 Original unchanged New array
let arr = [10, 20, 30, 40, 50];
let chunk = arr.slice(1, 4);
// chunk is [20, 30, 40]
// arr is still [10, 20, 30, 40, 50]

splice vs. slice

splice slice
Mutates original? Yes No
Returns Removed elements New sub-array
2nd argument Delete count End index (exclusive)
Can insert? Yes No
Use case Modify in place Read a portion safely

Choosing the Right Method

1
Identify the position: Are you working at the front, the end, or somewhere in the middle?
2
Identify the action: Are you adding, removing, replacing, or just reading?
3
Check mutation needs: Must the original array change, or do you need a copy?
4
Pick the method: End: push/pop. Front: unshift/shift. Middle: splice. Copy: slice.

Example 1: Building a Queue

A queue follows first-in, first-out (FIFO). Add to the end with push, remove from the front with shift.

let queue = [];

queue.push("Alice");  // ["Alice"]
queue.push("Bob");    // ["Alice", "Bob"]
queue.push("Carol");  // ["Alice", "Bob", "Carol"]

let next = queue.shift();
// next is "Alice"
// queue is ["Bob", "Carol"]

Example 2: Replacing with splice

Replace two items in the middle of an array with two new ones.

let days = ["Mon", "??", "??", "Thu", "Fri"];

// Replace index 1 and 2 with correct values
days.splice(1, 2, "Tue", "Wed");

// days is ["Mon", "Tue", "Wed", "Thu", "Fri"]
// returned: ["??", "??"]

Example 3: Extracting with slice

Get the middle three scores without changing the original data.

const scores = [55, 72, 88, 91, 64];

const middle = scores.slice(1, 4);
// middle is [72, 88, 91]
// scores is still [55, 72, 88, 91, 64]

// Negative indices count from the end
const lastTwo = scores.slice(-2);
// lastTwo is [91, 64]

Common Mistakes

✕ Confusing splice and slice
Using slice when you meant splice (or vice versa) causes silent bugs because one mutates and the other doesn't.
✕ Ignoring return values
pop() and shift() return the removed element. push() returns the new length. Forgetting this wastes useful data.
✕ Off-by-one with slice
slice(1, 3) gives indices 1 and 2, not 1, 2, and 3. The end index is always exclusive.

Method Summary

Method Action Position Returns
push Add End New length
pop Remove End Removed item
unshift Add Front New length
shift Remove Front Removed item
splice Remove / Insert / Replace Any index Removed items
slice Copy portion Any range New array

Question 1 of 5

What does arr.pop() return when arr = [10, 20, 30]?

Question 2 of 5

After running arr.unshift("x") on arr = ["a", "b"], what is arr?

Question 3 of 5

Given arr = [1, 2, 3, 4, 5], what does arr.slice(2, 4) return?

Question 4 of 5

After arr.splice(1, 2, "x", "y") on arr = ["a", "b", "c", "d"], what is arr?

Question 5 of 5

Which pair of methods implements a stack (last-in, first-out)?