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

Array.prototype.pop() β€” returns undefined for empty array

πŸ“– Usage Example
const arr = []
arr.pop()  // β†’ undefined  (empty array)

pop() returns undefined when called on an empty array β€” guard against this if needed.

Array.prototype.pop() β€” empty array

Calling pop() on an empty array does nothing and returns undefined. The array remains empty and no error is thrown.

Challenge: Implement popEmpty() that calls pop() on an empty array and returns the result.

popEmpty() // β†’ undefined
πŸ’‘ Hints (2)
  • β€’ Calling `pop()` on `[]` is safe β€” it returns `undefined` without throwing.
  • β€’ Cast with `([] as number[]).pop()` to satisfy the TypeScript return type.