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.
- For Adding one or more elements in the array.
- Returns the length of the new array.
- Push() adds elements to the end.
- 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:
- Both are used for removing.
- Returns the item that was removed.
- Pop() removes from the end.
- 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.
Top comments (0)