Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Adding Elements with push and unshift | Getting Started with Array Basics
JavaScript Array Methods

bookAdding Elements with push and unshift

When working with JavaScript arrays, you often need to add new elements either to the end or the beginning of an array. The push and unshift methods are designed for these tasks, but each serves a distinct purpose.

The push method appends one or more elements to the end of an array, making it ideal when you want to add items in the order they arrive, such as building a list of recent messages or appending new data records.

On the other hand, unshift inserts elements at the start of an array, which is useful when the most recent items should appear first, like a task list where new tasks need immediate attention.

Choosing between push and unshift depends on whether your use case requires new elements at the tail or the head of the array.

1234567
const fruits = []; fruits.push("apple"); // ["apple"] fruits.push("banana"); // ["apple", "banana"] fruits.unshift("orange"); // ["orange", "apple", "banana"] fruits.push("grape"); // ["orange", "apple", "banana", "grape"] fruits.unshift("kiwi"); // ["kiwi", "orange", "apple", "banana", "grape"] console.log(fruits);
copy

Both push and unshift not only modify the array but also return the new length of the array after the operation. For example, if you call push on an array with three elements, the return value will be 4 after adding one more. This is helpful if you need to track the size of your array as you add items. Every time you use push or unshift, the array grows by the number of elements you add, and the order of elements shifts accordingly.

Remember that push adds to the end, so earlier elements keep their positions, while unshift adds to the beginning and moves all existing elements up by one or more positions.

1. Which method would you use to add a new notification to the beginning of a notifications array so that it appears first?

2. Fill in the blanks to add "pear" to the end and "melon" to the beginning of the basket array.

question mark

Which method would you use to add a new notification to the beginning of a notifications array so that it appears first?

Select the correct answer

question-icon

Fill in the blanks to add "pear" to the end and "melon" to the beginning of the basket array.

const basket = ["apple", "banana"]; basket.("pear"); // add to end basket.("melon"); // add to beginning console.log(basket);
["melon", "apple", "banana", "pear"]

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain the difference between push and unshift with more examples?

What happens if I use push and unshift with multiple elements at once?

Are there performance differences between using push and unshift on large arrays?

Awesome!

Completion rate improved to 8.33

bookAdding Elements with push and unshift

Swipe to show menu

When working with JavaScript arrays, you often need to add new elements either to the end or the beginning of an array. The push and unshift methods are designed for these tasks, but each serves a distinct purpose.

The push method appends one or more elements to the end of an array, making it ideal when you want to add items in the order they arrive, such as building a list of recent messages or appending new data records.

On the other hand, unshift inserts elements at the start of an array, which is useful when the most recent items should appear first, like a task list where new tasks need immediate attention.

Choosing between push and unshift depends on whether your use case requires new elements at the tail or the head of the array.

1234567
const fruits = []; fruits.push("apple"); // ["apple"] fruits.push("banana"); // ["apple", "banana"] fruits.unshift("orange"); // ["orange", "apple", "banana"] fruits.push("grape"); // ["orange", "apple", "banana", "grape"] fruits.unshift("kiwi"); // ["kiwi", "orange", "apple", "banana", "grape"] console.log(fruits);
copy

Both push and unshift not only modify the array but also return the new length of the array after the operation. For example, if you call push on an array with three elements, the return value will be 4 after adding one more. This is helpful if you need to track the size of your array as you add items. Every time you use push or unshift, the array grows by the number of elements you add, and the order of elements shifts accordingly.

Remember that push adds to the end, so earlier elements keep their positions, while unshift adds to the beginning and moves all existing elements up by one or more positions.

1. Which method would you use to add a new notification to the beginning of a notifications array so that it appears first?

2. Fill in the blanks to add "pear" to the end and "melon" to the beginning of the basket array.

question mark

Which method would you use to add a new notification to the beginning of a notifications array so that it appears first?

Select the correct answer

question-icon

Fill in the blanks to add "pear" to the end and "melon" to the beginning of the basket array.

const basket = ["apple", "banana"]; basket.("pear"); // add to end basket.("melon"); // add to beginning console.log(basket);
["melon", "apple", "banana", "pear"]

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 2
some-alt