🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Array.pop()

AI & DATA SCIENCE // pop

pop() removes and returns the LAST element of an array. Mutates the original array.

Syntax

const last = array.pop();

Deep Dive Course

**pop()** removes the last element and returns it. It mutates the array. Together with **push()**, it implements a **stack** (LIFO — Last In, First Out). For a queue (FIFO), use **push()** and **shift()**.

1Understanding Array.pop()

pop() removes the last element and returns it. It mutates the array. Together with push(), it implements a stack (LIFO — Last In, First Out). For a queue (FIFO), use push() and shift().

💡

pop() on an empty array returns undefined without throwing an error.

editor.html
// Stack implementation with push/pop
const history = [];

function navigate(url) { history.push(url); }
function goBack()     { return history.pop(); }

navigate('/home');
navigate('/about');
navigate('/contact');
console.log(history); // ['/home', '/about', '/contact']
console.log(goBack()); // '/contact'
console.log(history);  // ['/home', '/about']
localhost:3000

2Practical Example

Here is a real-world application of Array.pop() showing how it is used in production JavaScript code.

editor.html
// Non-mutating last removal
const arr = [1, 2, 3, 4, 5];
const withoutLast = arr.slice(0, -1);
console.log(arr);          // [1,2,3,4,5] unchanged
console.log(withoutLast);  // [1,2,3,4]
localhost:3000

3Best Practices

Follow these guidelines when working with Array.pop():

1. Use push()/pop() to implement stacks

2. Check array.length before popping if empty arrays are possible

3. Use slice(0, -1) for non-mutating removal of last element

⚠️

Tip: pop() on an empty array returns undefined without throwing an error.

editor.html
// Stack implementation with push/pop
const history = [];

function navigate(url) { history.push(url); }
function goBack()     { return history.pop(); }

navigate('/home');
navigate('/about');
navigate('/contact');
console.log(history); // ['/home', '/about', '/contact']
console.log(goBack()); // '/contact'
console.log(history);  // ['/home', '/about']
localhost:3000

Examples

Example 01Basic Usage
// Stack implementation with push/pop
const history = [];

function navigate(url) { history.push(url); }
function goBack()     { return history.pop(); }

navigate('/home');
navigate('/about');
navigate('/contact');
console.log(history); // ['/home', '/about', '/contact']
console.log(goBack()); // '/contact'
console.log(history);  // ['/home', '/about']
Example 02Advanced Example
// Non-mutating last removal
const arr = [1, 2, 3, 4, 5];
const withoutLast = arr.slice(0, -1);
console.log(arr);          // [1,2,3,4,5] unchanged
console.log(withoutLast);  // [1,2,3,4]

Best Practices

  • Use push()/pop() to implement stacks
  • Check array.length before popping if empty arrays are possible
  • Use slice(0, -1) for non-mutating removal of last element

Interview Question

How would you implement a stack using an array?

Hint: push for add, pop for remove.

An array with push() (add to top) and pop() (remove from top) is a stack. The last element pushed is the first popped (LIFO). For a bounded stack, check length before pushing. For a queue (FIFO), use push() to enqueue and shift() to dequeue.

Exercises

MediumPractice using Array.pop() in a real scenario.
View Solution
// Stack implementation with push/pop
const history = [];

function navigate(url) { history.push(url); }
function goBack()     { return history.pop(); }

navigate('/home');
navigate('/about');
navigate('/contact');
console.log(history); // ['/home', '/about', '/contact']
console.log(goBack()); // '/contact'
console.log(history);  // ['/home', '/about']

Frequently Asked Questions

How would you implement a stack using an array?

An array with push() (add to top) and pop() (remove from top) is a stack. The last element pushed is the first popped (LIFO). For a bounded stack, check length before pushing. For a queue (FIFO), use push() to enqueue and shift() to dequeue.

Related Functions

pushshiftunshiftArray-Methods