Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • 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 *

  • JavaScript Array Methods
  • JavaScript Array.from() Method
  • JavaScript Array isArray() Method
  • JavaScript Array at() Method
  • JavaScript Array every() Method
  • JavaScript Array filter() Method
  • JavaScript Array find() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array indexOf() Method
  • JavaScript Array join() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method
  • JavaScript Array map() Method
  • JavaScript Array pop() Method
  • JavaScript Array push() Method
  • JavaScript Array reduce() Method
  • JavaScript Array reduceRight() Method
  • JavaScript Array reverse() Method
  • JavaScript Array shift() Method
  • JavaScript Array slice() Method
  • JavaScript Array some() Method
  • JavaScript Array sort() Method
  • JavaScript Array splice() Method
  • JavaScript Array toLocaleString() Method
  • JavaScript Array toReversed() Method
  • JavaScript Array toSorted() Method
  • JavaScript Array toSpliced() Method
  • JavaScript Array toString() Method
  • JavaScript Array unshift() Method
  • JavaScript Array values() Method
  • JavaScript Array with() Method

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme