Pop and Push 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 pop() and push() methods. In my next post I will cover how we can leverage the shift() and unshift() methods.

push(): add an item to the end of an array

let kids = ['Alex'];

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

kids.push('Puff', 'George'); // ['Alex', 'Anika', 'Puff', 'George']

push() returns the new array’s length

pop(): Remove an item from the end of an array

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

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

pop() returns the item that was removed.

Leave a Reply

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