JavaScript array push, pop, shift, unshift: how to remember the difference

yaphi1

Yaphi Berhanu

Posted on August 7, 2017

JavaScript array push, pop, shift, unshift: how to remember the difference

When you're adding and removing stuff from the beginning and end of a JavaScript array, it can be tough to remember which method does what. Here are some tips:

  • Unshift and shift make the whole array shift sideways (by adding and removing items from the beginning).

  • Push and pop do NOT make the array shift sideways (because they add and remove items at the end).

  • In each of those pairs (push/pop and unshift/shift), the longer word makes the array longer.

If you're curious, I made a quick animation to illustrate these points.

I hope this helps!


Note that this is more of a memorization trick than a full explanation, so feel free to check out MDN for full details and examples of push, pop, shift, and unshift.

💖 💪 🙅 🚩
yaphi1
Yaphi Berhanu

Posted on August 7, 2017

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related