loading...
Cover image for JavaScript array push, pop, shift, unshift: how to remember the difference

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

yaphi1 profile image Yaphi Berhanu Originally published at simplestepscode.com ・1 min read

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.

Posted on Jul 6 '17 by:

yaphi1 profile

Yaphi Berhanu

@yaphi1

I like to make JavaScript simple when I can. I hope it's helpful!

Discussion

markdown guide
 

Bookmarked!!!
Being a rather visually-focused learner, your linked animation is probably the coolest thing I've seen this week! Great concept, very clear... and something I'm sure I'll retain!
Nicely done :)

 

Glad the animation was helpful!