Lecture

Array Methods - push, pop, unshift, shift

Let's learn about methods for managing elements within an array.


array.push

The push method adds new items to the end of an array. It's like adding a card to the top of a deck of cards.

Example of push method
const fruits = ['apple', 'banana']; fruits.push('cherry'); console.log(fruits); // Prints ["apple", "banana", "cherry"]

array.pop

The pop method removes the last item from an array and returns that item. It's like taking the top card from a deck of cards.

Example of pop method
const fruits = ['apple', 'banana', 'cherry']; const lastFruit = fruits.pop(); console.log(lastFruit); // Prints "cherry" console.log(fruits); // Prints ["apple", "banana"]

array.unshift

The unshift method adds new items to the beginning of an array. It's like inserting a card at the bottom of a deck of cards.

Example of unshift method
const fruits = ['banana', 'cherry']; fruits.unshift('apple'); console.log(fruits); // Prints ["apple", "banana", "cherry"]

array.shift

The shift method removes the first item from an array and returns that item. It's like taking the bottom card from a deck of cards.

Example of shift method
const fruits = ['apple', 'banana', 'cherry']; const firstFruit = fruits.shift(); console.log(firstFruit); // Prints "apple" console.log(fruits); // Prints ["banana", "cherry"]
Mission
0 / 1

What does the array.pop method do?

Returns the first item of the array.

Adds an item at the beginning of the array.

Returns the last item of the array.

Adds an item at the end of the array.

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

Code Editor

Run

Execution Result