JavaScript Code

JavaScript Array pop()


pop() – Remove Last Element

array.pop()
  • array is an array of elements
  • pop is the array method name

pop() method removes the last element of the given array and returns the popped element. pop() method modifies the original array

Examples

1. remove the last element of the array numbers

let numbers = [5, 1, 7, 3, 2];
let removed = numbers.pop();
console.log('Popped element: ' + removed);
console.log(numbers);

2. remove the element from an empty array

let numbers = [];
let removed = numbers.pop();
console.log('Popped element: ' + removed);
console.log(numbers);

if the array is empty, pop() returns undefined


Copyright @2022