Shift and Unshift Array Methods in JavaScript

JavaScript gives us four built-in methods to add and remove items to the beginning and the end of array.

In this post we will examine the shift() and unshift() methods. As with the previously covered methods, shift() and unshift() are great adding and removing items to and from an array.

shift(): removes an item from the beginning of an array

let kids = ['Alex', 'Anika', 'Paulina'];

kids.shift('Anika'); // ['Alex', 'Anika']

shift() returns the removed item.

unshift(): adds an item to the beginning of an array

let kids = ['Anika', 'Paulina'];

kids.unshift('Alex'); // ['Alex', 'Anika', 'Paulina']

unshift() returns the new array’s length.

Leave a Reply

Your email address will not be published. Required fields are marked *