How to use push(), unshift(),pop() and shift() with array in javascript

Nuthan Murarysetty
2 min readMay 27, 2020

--

Hello Readers, As the title gives you an idea of what is going to learn from this article.

We have a lot of functions, properties in any language and functionality-wise doing the same but there should be at least one difference.

As per the above statement, We use four functions in javascript and describe the functionality of those functions.

Here functionality is inserted and deleted from an array. Now, A question arises in your mind. Why we need 4 functions?

Let’s go in deeper to learn more about this,

As in array, We store data in memory associated with an index (Starting index is 0) to fetch the value.

Insert a new element we use push() and unshift() functions in javascript. Please look into an example in below screenshot,

Difference between push() vs unshift() functions in javascript
push() vs unshift() functions in javascript

Removing/Deleting existing element we use pop() and shift() functions in javascript. Please look into an example in below screenshot,

Difference between pop() vs shift() functions in javascript
pop() vs shift() functions in javascript

That’s all folks and hope with this article you cleared the differences between these functions in javascript.

Thanks for reading and will come up with basic and important concepts which we use but not clear in our day to day work.

— Happy Coding : )

--

--

Nuthan Murarysetty
Nuthan Murarysetty

Written by Nuthan Murarysetty

I love sharing things what I know to others and passionate in photography.

No responses yet