Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
JavaScript Array pop()
In this tutorial, we will learn about the JavaScript Array push() method with the help of examples.
The pop() method removes the last element from an array and returns that element.
Example
let cities = ["Madrid", "New York", "Kathmandu", "Paris"];
// remove the last element
let removedCity = cities.pop();
console.log(cities) // ["Madrid", "New York", "Kathmandu"]
console.log(removedCity); // Paris
pop() Syntax
The syntax of the pop() method is:
arr.pop()
Here, arr is an array.
pop() Parameters
The pop() method does not have any parameters.
pop() Return Value
- Removes the last element from
arrayand returns that value. - Returns
undefinedif the array is empty.
Notes:
- This method changes the original array and its length.
- To remove the first element of an array, use the JavaScript Array shift() method.
Example: Using pop() method
var languages = ["JavaScript", "Python", "Java", "C++", "Lua"];
var popped = languages.pop();
console.log(languages); // [ 'JavaScript', 'Python', 'Java', 'C++' ]
console.log(popped); // Lua
// pop returns any type of object
var numbers = [
[1, 2, 3],
[4, 5, 6],
[-5, -4, -3],
];
console.log(numbers.pop()); // [ -5, -4, -3 ]
console.log(numbers); // [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
Output
[ 'JavaScript', 'Python', 'Java', 'C++' ] Lua [ -5, -4, -3 ] [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
Also Read:
Did you find this article helpful?