Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Removing Elements with pop and shift | Getting Started with Array Basics
JavaScript Array Methods

bookRemoving Elements with pop and shift

12345
// Using pop to remove the last element from an array const fruits = ["apple", "banana", "cherry"]; const lastFruit = fruits.pop(); // Removes "cherry" from the end console.log(fruits); // Output: ["apple", "banana"] console.log(lastFruit); // Output: "cherry"
copy
12345
// Using shift to remove the first element from an array const vegetables = ["carrot", "lettuce", "tomato"]; const firstVegetable = vegetables.shift(); // Removes "carrot" from the start console.log(vegetables); // Output: ["lettuce", "tomato"] console.log(firstVegetable); // Output: "carrot"
copy

When you use pop or shift, you are directly modifying the array. The element at the end (pop) or the start (shift) is removed, and the method returns that element. This is useful when you need to process elements sequentially from either end of an array, such as implementing queues or stacks. It is important to remember that both methods change the original array, so if you need to keep the original data intact, you should make a copy before using these methods.

In summary, pop and shift are used to remove elements from arrays, while push and unshift add elements. pop and push work at the end of the array, and shift and unshift work at the beginning. This gives you flexible control over how you add or remove items from your arrays, making it easy to build stack- or queue-like behaviors with simple method calls.

Note

  • Use pop to remove the last element from an array;
  • Use push to add one or more elements to the end of an array;
  • Use shift to remove the first element from an array;
  • Use unshift to add one or more elements to the beginning of an array.

1. Which statement describes the result of running the following code?

2. Fill in the blank to remove the last element from the array using the correct method.

question mark

Which statement describes the result of running the following code?

Select the correct answer

question-icon

Fill in the blank to remove the last element from the array using the correct method.

const pets = ["dog", "cat", "hamster"]; const gone = pets.(); console.log(pets); // Should output: ["dog", "cat"] console.log(gone); // Should output: "hamster"
["dog", "cat"]
"hamster"

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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 3

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Awesome!

Completion rate improved to 8.33

bookRemoving Elements with pop and shift

Veeg om het menu te tonen

12345
// Using pop to remove the last element from an array const fruits = ["apple", "banana", "cherry"]; const lastFruit = fruits.pop(); // Removes "cherry" from the end console.log(fruits); // Output: ["apple", "banana"] console.log(lastFruit); // Output: "cherry"
copy
12345
// Using shift to remove the first element from an array const vegetables = ["carrot", "lettuce", "tomato"]; const firstVegetable = vegetables.shift(); // Removes "carrot" from the start console.log(vegetables); // Output: ["lettuce", "tomato"] console.log(firstVegetable); // Output: "carrot"
copy

When you use pop or shift, you are directly modifying the array. The element at the end (pop) or the start (shift) is removed, and the method returns that element. This is useful when you need to process elements sequentially from either end of an array, such as implementing queues or stacks. It is important to remember that both methods change the original array, so if you need to keep the original data intact, you should make a copy before using these methods.

In summary, pop and shift are used to remove elements from arrays, while push and unshift add elements. pop and push work at the end of the array, and shift and unshift work at the beginning. This gives you flexible control over how you add or remove items from your arrays, making it easy to build stack- or queue-like behaviors with simple method calls.

Note

  • Use pop to remove the last element from an array;
  • Use push to add one or more elements to the end of an array;
  • Use shift to remove the first element from an array;
  • Use unshift to add one or more elements to the beginning of an array.

1. Which statement describes the result of running the following code?

2. Fill in the blank to remove the last element from the array using the correct method.

question mark

Which statement describes the result of running the following code?

Select the correct answer

question-icon

Fill in the blank to remove the last element from the array using the correct method.

const pets = ["dog", "cat", "hamster"]; const gone = pets.(); console.log(pets); // Should output: ["dog", "cat"] console.log(gone); // Should output: "hamster"
["dog", "cat"]
"hamster"

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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 3
some-alt