JS Built-ins
← Array137 / 226
ArrayArray.prototype.popbeginner

Array.prototype.pop() β€” remove and return last element

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

Use pop() to remove and return the last element of an array.

Array.prototype.pop()

Array.prototype.pop() removes the last element from an array and returns it. The array is mutated in place (its length decreases by 1).

Challenge: Implement removeLast(arr) that pops and returns the removed element.

removeLast([1, 2, 3]) // β†’ 3  (arr is now [1, 2])
removeLast([42])      // β†’ 42 (arr is now [])
πŸ’‘ Hints (2)
  • β€’ `pop()` both mutates the array and returns the removed element.
  • β€’ The element returned is the one at index `arr.length - 1`.