Skip to content

WebDevHubs

  • Home
  • JavaScript
  • 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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 20, 30, 40, 50];
let lastItem = arr.pop();
console.log(lastItem); // Output: 50
console.log(arr); // Output: [10, 20, 30, 40]
let arr = [10, 20, 30, 40, 50]; let lastItem = arr.pop(); console.log(lastItem); // Output: 50 console.log(arr); // Output: [10, 20, 30, 40]
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [];
let poppedItem = arr.pop();
console.log(poppedItem); // Output: undefined
console.log(arr); // Output: []
let arr = []; let poppedItem = arr.pop(); console.log(poppedItem); // Output: undefined console.log(arr); // Output: []
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 20, 30, 40, 50];
while (arr.length > 0) {
let lastItem = arr.pop();
console.log(lastItem);
}
/* Output:
50
40
30
20
10
*/
let arr = [10, 20, 30, 40, 50]; while (arr.length > 0) { let lastItem = arr.pop(); console.log(lastItem); } /* Output: 50 40 30 20 10 */
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [1, , 3, , 5];
let lastItem = arr.pop();
console.log(lastItem); // Output: 5
console.log(arr); // Output: [1, , 3, ]
let arr = [1, , 3, , 5]; let lastItem = arr.pop(); console.log(lastItem); // Output: 5 console.log(arr); // Output: [1, , 3, ]
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

More Related Articles

How to Remove Item from an Array by Value in JavaScript? JavaScript
How to Merge (Flatten) an Array of Arrays in JavaScript? JavaScript
Lodash _.compact() Method JavaScript
JavaScript Array unshift() Method JavaScript
Remove the Last Element of an Array in JavaScript JavaScript
How to Insert an Item into an Array at a Specific Index in JavaScript? JavaScript

Leave a Reply Cancel reply

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

Archives

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

Categories

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

Recent Posts

  • How to Center Text in HTML?
  • Design a Simple HTML Page | First HTML Project
  • Best Way to Initialize an Empty Array in PHP
  • HTML Description Lists
  • HTML Ordered Lists

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme