Skip to main content

JavaScript Array pop Method (Live Playground)

The pop method in JavaScript is used to remove the last element from an array.

Let's consider a simple example:

let numbers = [1, 2, 3, 4, 5];

let lastNumber = numbers.pop();

console.log(lastNumber);
// Expected output: 5
console.log(numbers);
// Expected output: [1, 2, 3, 4]

In this example, the pop method removes the last element (5) from the numbers array. The pop method then returns the removed element.

Live Playground, Try it Yourself

Unlike some other array methods, pop modifies the original array, effectively decreasing its length by one:

let numbers = [1, 2, 3, 4, 5];

numbers.pop();

console.log(numbers.length);
// Expected output: 4

In the above example, after calling the pop method, the length of the numbers array is decreased to 4.

Live Playground, Try it Yourself

In conclusion, the pop method in JavaScript is a simple and effective way to remove the last element from an array. Whether you're handling data in an application or performing complex manipulations, the pop method is an essential part of your JavaScript arsenal.

Remember, the best way to understand pop is by using it in different scenarios and observing the results!