Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
JavaScript

JavaScript Array pop(): Removing the Last Element

  • 23 Aug, 2025
  • Com 0
JavaScript Array pop() Method

JavaScript Array pop() method removes the last element from an input array and returns the removed element.

It modifies the original array in place by reducing its length property by 1.

Remove the last element from an array using Array.pop() method in JavaScript

let countries = ["UK", "US", "India", "Israel", "Mexico"];

console.log('length of countries array before popping: ', countries.length);

// Output: length of countries array before popping:  5

let removedItem = countries.pop();

console.log('The remaining items are: ', countries);

// Output: The remaining items are:  [ 'UK', 'US', 'India', 'Israel' ]

console.log('The removed item is: ', removedItem);

// Output: The removed item is:  Mexico

console.log('length of countries array: ', countries.length);

// Output: length of countries array:  4

Syntax

array.pop()

Parameters

It does not take any argument.

Empty array

If you try to pop an element from an empty array, it returns undefined.

Popping an element from an empty array in JavaScript

let countries = [];

let removedItem = countries.pop();

console.log(removedItem);

// Output: undefined

console.log(countries.length);

// Output: 0

Array with a single element

If the input array contains a single element and you remove that, the array becomes empty.

let single = [42];
let removed = single.pop();

console.log(removed);

// Output: 42

console.log(single);

// Output: []

console.log(single.length);

// Output: 0

Removing an element from an array of objects

Let’s say you have an array filled with different objects. You can remove the last object from an array using the pop() method. No matter whether the element is a number, string, or object, the pop() works the same.

let apps = [
    { name: 'Amazon', value: 1 },
    { name: 'Amazon Prime', value: 21 },
    { name: 'Amazon Music', value: 10 },
    { name: 'Amazon Go', value: 100 },
    { name: 'Amazon Alexa', value: 13 }
];

removedItem = apps.pop();

console.log('The remaining items are: ', apps);

// Output: The remaining items are:
//  [
//   { name: 'Amazon', value: 1 },
//   { name: 'Amazon Prime', value: 21 },
//   { name: 'Amazon Music', value: 10 },
//   { name: 'Amazon Go', value: 100 }
// ]

console.log('The removed item is: ', removedItem);

// Output: The removed item is:  { name: 'Amazon Alexa', value: 13 }

As you can see from the output, the removed element is the last object.

Clearing the whole array

You can clear the whole array using a for loop and the pop() method. Just iterate through each element and remove the item one by one, unless the entire array becomes empty.

let stack_array = [11, 19, 3, 21];

while (stack_array.length > 0) {
    stack_array.pop();
}

console.log("Final Stack:", stack_array);

// Output: Final Stack: []

Sparse arrays

A sparse array is an array with holes in it. Holes, meaning empty slots. An empty slot counts as an element in the array.

Applying pop() method on Sparse arrays in JavaScript

const sparse_array = ['k', , 'b', , 'l']; // Holes at index 1 and 3

console.log(sparse_array.length);

// Output: 5

sparse_array.pop(); // Removes 'l'

console.log(sparse_array);

// Output: [ 'k', <1 empty item>, 'b', <1 empty item> ]

console.log(sparse_array.length);

// Output: 4

The sparse array’s length is 5, apparently because empty slots are counted. After the removal of the last element, the updated length is 4.

Post Views: 12
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

JavaScript Array find() Method
How to Empty an Array in JavaScript

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

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

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend