What is the problem with const rows = new Array(3).fill([]);?
fill copies the reference into every slot. Array.from({ length: 3 }, () => []) creates a new array per slot.
Arrays · Exercise 1 of 4
Fifteen multiple-choice questions on JavaScript arrays: creating arrays, push and pop return values, slice vs splice, sort, length and holes, at(), indexOf vs includes, copying, fill and the ES2023 non-mutating methods. Pick an option to check your answer instantly.
What is the problem with const rows = new Array(3).fill([]);?
fill copies the reference into every slot. Array.from({ length: 3 }, () => []) creates a new array per slot.
Which method changes the array it is called on?
reverse (like sort, splice, push and fill) works in place and returns the same array. slice, concat and toSorted return new arrays.
On an array with a million elements, why is unshift(x) usually much slower than push(x)?
Adding or removing at the start (unshift/shift) is O(n); at the end (push/pop) it is O(1) amortised. Queues with heavy shift use are better built differently.
What is the difference between arr.sort() and arr.toSorted()?
toSorted (ES2023) accepts the same compare function. Before it, the idiom was [...arr].sort(compare).
Which of these can Array.from turn into a real array?
Array.from accepts any iterable or array-like object, plus an optional map function: Array.from("abc"), Array.from(new Set(list)), Array.from({ length: 3 }, (_, i) => i).
Timed online tests with instant scores and explanations.