Pop, Push, Shift and Unshift (JavaScript)

Joseph Perez
Jun 5, 2021

--

JavaScript gives us four methods to add or remove items from the beginning or end of arrays:

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

let cats = ['Bob', 'Willy', 'Mini'];cats.pop(); // ['Bob', 'Willy']

pop() returns the removed item.

push(): Add items to the end of an array

let cats = ['Bob'];cats.push('Willy'); // ['Bob', 'Willy']cats.push('Puff', 'George'); // ['Bob', 'Willy', 'Puff', 'George']

push() returns the new array length.

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

let cats = ['Bob', 'Willy', 'Mini'];cats.shift(); // ['Willy', 'Mini']

shift() returns the removed item.

unshift(): Add items to the beginning of an array

let cats = ['Bob'];cats.unshift('Willy'); // ['Willy', 'Bob']cats.unshift('Puff', 'George'); // ['Puff', 'George', 'Willy', 'Bob']

unshift() returns the new array length.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Joseph Perez
Joseph Perez

Written by Joseph Perez

I am a software engineer working in EdTech. I have a passion for supporting those working hard to get into the tech industry.

No responses yet

Write a response