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.
Start with const tasks = ["wash", "cook"];. Add "shop" at the start and "sleep" at the end, then remove "cook" with splice. Print the array.
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
What do push, pop, shift and unshift return?
Explain the arguments and return value of splice.
What is the difference between a mutating and a non-mutating array method, and why does it matter?
Why is shift slower than pop on a large array?
Preview
Console
Nothing logged yet.Expected console output
3
Meera
3
Dev
[ 'Asha', 'Ravi' ]
undefined