Essential JavaScript

Removing Elements

We can remove elements from an existing array.

We use the pop and shift methods to remove elements from the beginning or end of the array.

Both methods return the item they removed.

We can use these methods to process the items in an array one-by-one.

Pop from the End

The pop method removes an element from end of the array and returns it:

const arr = [1, 2, 3, 4];
const lastItem = arr.pop();
console.log(arr);
console.log(lastItem);

Shift from the Start

The shift method removes an element from the start of an array and returns it:

const arr = [1, 2, 3, 4];
const firstItem = arr.shift();
console.log(arr);
console.log(firstItem);

Array Length

Both of these methods change the number of elements in the array.

The array's length property will automatically update:

const arr = [1, 2, 3];
console.log(arr.length); // 3
arr.pop();
console.log(arr); // [1, 2]
console.log(arr.length); // 2

Learning Goals

Code Editor