JavaScript Code


JavaScript Array pop()


Remove Last Element from Array in JavaScript

In JavaScript, Array.pop() method is used to remove and return the last element from array.

Syntax

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