← All references

Array push/pop

array.push(item) / array.pop() / array.shift() / array.unshift()

Add or remove items from an array.

Array push/pop

  • push(x) — add x to the end, returns new length.
  • pop() — remove the last item, returns it.
  • unshift(x) — add x to the start.
  • shift() — remove the first item, returns it.

All four mutate the original array.

Example

const fruits = ["apple", "banana"];\nfruits.push("cherry");\nconsole.log(fruits);\nconst last = fruits.pop();\nconsole.log("Popped:", last);\nfruits.unshift("orange");\nconsole.log(fruits);