Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 5.2 — Adding, Removing and Changing
JavaScript · Example 1 of 3

A queue at the counter

Click the buttons: joining uses push (add at the back), serving uses shift (remove from the front). The console part shows what each method returns.

push/pop work at the end of an array, unshift/shift at the start, and splice anywhere in the middle; all of them change (mutate) the original array, while methods like slice, concat, with and toSpliced leave it alone and return a new one.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Start with const tasks = ["wash", "cook"];. Add "shop" at the start and "sleep" at the end, then remove "cook" with splice. Print the array.

02

Write a function removeAt(arr, i) that returns a new array without the item at index i and doesn't change arr.

Explain it without notes

01

What do push, pop, shift and unshift return?

02

Explain the arguments and return value of splice.

03

What is the difference between a mutating and a non-mutating array method, and why does it matter?

04

Why is shift slower than pop on a large array?

A queue at the counter

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

3
Meera
3
Dev
[ 'Asha', 'Ravi' ]
undefined