Code Highlights logo

Learn

Arrays

The .pop() Method

The .pop() method does two things:

  1. Removes the last element from the given array.
  2. Returns the removed element as the result of the method call.

Syntax:

1let removedItem = myArray.pop();

Let's dive into some code examples to understand the .pop() method better:

1let fruits = ['apple', 'banana', 'orange'];
2let lastFruit = fruits.pop();
3
4console.log(fruits); // Output: ['apple', 'banana']
5console.log(lastFruit); // Output: 'orange'

As you can see in the example, the .pop() method removes the last element 'orange' from the fruits array and returns it as the result. The fruits array now contains only 'apple' and 'banana'.

It's important to note that the .pop() method permanently modifies the original array. If you want to keep the removed element, you should save it in a separate variable, as shown in the example.


Instructions

1.

Declare a variable called removedFruit and assign it the value of using the .pop() method on the fruits array.

2.

Log the removedFruit variable to the console.

Sign up to start coding

Already have an account?

Sign In

Course content