DEV Community

Cover image for Push, Pop, Shift, Unshift : An Easy way to remember
Arun Prakash Pandey
Arun Prakash Pandey

Posted on

Push, Pop, Shift, Unshift : An Easy way to remember

JavaScript can be confusing in the beginning and this article will help you to easily remember & differentiate between the push(), pop(), shift() and unshift() methods.

Push() and Unshift()-> 'u' is common

The trick is to observe that the letter U is used to spell push and unshift. Hence, they have similar use cases.

  1. For Adding one or more elements in the array.
  2. Returns the length of the new array.
  3. Push() adds elements to the end.
  4. Unshift() adds elements to the beginning.

Now, you know an easier way to remember push() and unshift() methods. The pop and shift can be easily associated with each other.

Pop() and Shift() ->

The trick is to see that unlike push() and unshift() method (where the letter 'u' is common). Nothing is matching here. So, we can keep the pop and shift method in one group. Their use cases are similar as follows:

  1. Both are used for removing.
  2. Returns the item that was removed.
  3. Pop() removes from the end.
  4. Shift() removes from the start of the array.

I hope this helps you to easily remember the methods and their uses.
Feel free to reach out for feedback (good or bad), or just to connect.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay