What does console.log([1, 2, 3, 4, 5].slice(-2)); output?
A past puzzle — fully playable. 4 attempts, hints on wrong guesses.
stdle-51.js
1console.log([1,2,3,4,5].slice(-2));
Quirks
Answer & explanation
Console output
[ 4, 5 ]
Why
slice accepts negative indices that count backward from the end of the array, so -2 means "start two elements before the end." It returns a shallow copy of that range, here the last two elements [4, 5], and, unlike splice, never mutates the original array. This makes slice(-n) a clean way to grab the last n items.