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

Arun Prakash Pandey @d32ssv

About: Full Stack Dev in MERN, transitioned from non-tech

Location:
New Delhi, India
Joined:
May 17, 2023

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

Publish Date: Aug 23 '23
0 0

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.

Comments 0 total

    Add comment