EasyCode365EasyCode365

Interactive Lesson: push(), pop(), unshift(), and shift()

This lesson is focused on practice.

You already know that arrays store multiple values in one variable. Now you will see how four common methods change an array step by step.

What you will practice

  • push() adds an item to the end.
  • pop() removes the last item.
  • unshift() adds an item to the beginning.
  • shift() removes the first item.

These methods change the original array.

Try the interactive playground

Type a value, then choose where to add it. You can also remove items from the beginning or the end.

Watch the index numbers under each item. When an item is added or removed at the beginning, the indexes of the other items change.

Array methods playground

length: 3
start[
a
[0]
b
[1]
c
[2]
end]

Add an item

Remove an item

Mini task

Use the playground to do these steps:

  1. Add "x" to the end with push().
  2. Add "start" to the beginning with unshift().
  3. Remove the last item with pop().
  4. Remove the first item with shift().

After each step, look at the array length and the index labels.