Learn
Arrays
The .pop() Method
The .pop()
method does two things:
- Removes the last element from the given array.
- Returns the removed element as the result of the method call.
Syntax:
Let's dive into some code examples to understand the .pop()
method better:
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