Faizah Ahsan
2 min readDec 17, 2020

Shift Vs Unshift Methods in Javascript!

If you are new in the software developer world, these two Shift() & Unshift() methods Are very important. You will probably be using these methods inside a lot of functions to solve a lot of problems. So let’s find out what each of these actually does and how they differ from each other.

.shift() Method

The .shift() method removes the first items of and array. This method changes the length of an array. The return value of the shift method is the removed item. This method will change the original array. If the array is empty is it will return undefined. After removing the element at 0th index, it shifts other values to consecutive indexes down. The .shift() method does not have any parameters.

The Syntax of the .shift() method is:

array.shift()

.unshift() Method

Javascript unshift() method is like the push() method, only it works at the beginning of the array. This method can prepend one or more elements to the beginning of an array. This alters an array on which the method was called. Unshift() is an inbuilt function that adds one or more items or elements to the beginning of the array and returns the new length of the array. The unshift() method changes the length of the array directly, so it is not a pure function. The number of arguments to this function is the same as the number of elements to be added at the beginning of the array.

The Syntax of the .unshift() method is:

array.unshift()

Faizah Ahsan
Faizah Ahsan

Written by Faizah Ahsan

Junior Full-Stack Web Developer. Looking for opportunities!

No responses yet