JavaScript
Array Methods
Master the essential methods for adding, removing, and extracting elements from arrays: push, pop, shift, unshift, splice, and slice.
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.
The Two Ends of an Array
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.
push and pop
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
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.
let arr = ["a", "b", "c", "d", "e"];
arr.splice(1, 2);
// removed: ["b", "c"]
// arr is now ["a", "d", "e"]
splice: Insert and Replace
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"]
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.
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
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.
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.
// 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 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
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)?