JS Built-ins
← Array140 / 226
ArrayArray.prototype.popintermediate

Array.prototype.pop() β€” implement a stack pop

πŸ“– Usage Example
const stack = []
stack.push(1)
stack.push(2)
stack.pop()   // β†’ 2  (LIFO)
stack.pop()   // β†’ 1

Use push() and pop() together to implement a last-in, first-out (LIFO) stack.

Array.prototype.pop() β€” stack data structure

Arrays in JavaScript naturally model a stack (LIFO β€” last in, first out). push adds to the top; pop removes from the top. This is the classic stack-pop operation.

Challenge: Implement stackPop(stack) β€” a named stack-pop function that removes and returns the top element of the stack.

stackPop([1, 2, 3]) // β†’ 3  (stack is now [1, 2])
stackPop([99])      // β†’ 99 (stack is now [])
πŸ’‘ Hints (2)
  • β€’ A stack's "top" is the last element of the array.
  • β€’ `pop()` is the natural implementation of a stack pop β€” O(1) and mutation-based.