Skip to content

WebDevHubs

  • Home
  • HTML
  • CSS
  • JavaScript
  • Web Technologies
  • Web Templates
  • Toggle search form

JavaScript Array pop() Method

Posted on December 16, 2024 By Admin No Comments on JavaScript Array pop() Method

The Array.pop() method is a built-in JavaScript function that removes the last element from an array and returns that element. It modifies the original array and reduces its length by one.

Syntax

arr.pop();

Parameters

The pop() method does not take any parameters.

Return Value

The pop() method returns the last element of the array if the array is not empty and undefined if the array is empty.

Example 1: The pop() method removes the last element and returns it. The original array is updated.

let arr = [10, 20, 30, 40, 50];
let lastItem = arr.pop();
console.log(lastItem); // Output: 50
console.log(arr);    // Output: [10, 20, 30, 40]

Example 2: Using pop() on an Empty Array. When the array is empty, pop() returns undefined without causing an error.

let arr = [];

let poppedItem = arr.pop();
console.log(poppedItem); // Output: undefined
console.log(arr);        // Output: []

Example 3: The pop() method is used in a loop to process and remove tasks from the end of the array.

let arr = [10, 20, 30, 40, 50];

while (arr.length > 0) {
    let lastItem = arr.pop();
    console.log(lastItem);
}
/* Output:
50
40
30
20
10
*/

Example 5: Using pop() method on a Sparse Array

let arr = [1, , 3, , 5];

let lastItem = arr.pop();
console.log(lastItem);   // Output: 5
console.log(arr);        // Output: [1, , 3, ]

Supported Browsers

BrowserSupport
Chrome1+
Firefox1+
Safari1+
Edge12+
Opera4+
Internet Explorer5.5+
JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Array-Method, JavaScript-Method

Post navigation

Previous Post: JavaScript Array sort() Method
Next Post: JavaScript Array push() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Archives

  • June 2025
  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • JavaScript
  • Lodash
  • PHP
  • Python
  • Web Technologies
  • Web Templates

Recent Posts

  • JavaScript Array isArray() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme