In function total(...prices) { return Math.max(...prices); }, what are the two ...?
In a parameter list ... collects (rest); in a call it expands (spread).
Spread and Rest Operators · Exercise 1 of 4
Fifteen multiple-choice questions on JavaScript spread and rest: telling them apart, shallow copies, merging order, null and undefined, rest parameters vs arguments, conditional spread, what object spread copies, Object.assign and limits. Pick an option to check your answer instantly.
In function total(...prices) { return Math.max(...prices); }, what are the two ...?
In a parameter list ... collects (rest); in a call it expands (spread).
After const copy = [...matrix]; for a 2-D array, which statement is true?
Spread copies one level. Changing copy[0][0] changes matrix[0][0]. Use structuredClone or matrix.map((row) => [...row]).
What is { ...{ size: "M", qty: 1 }, ...{ size: "L" } }?
Properties are applied left to right; a later key overwrites an earlier one but keeps its original position.
What happens with { ...null } and [...null]?
Object spread ignores null and undefined. Array spread needs an iterable, and null is not iterable.
Which function definition is valid?
Only one rest parameter is allowed, it must be last, and no trailing comma may follow it.
Why are rest parameters preferred over the arguments object?
arguments is array-like (no array methods), includes all arguments and does not exist in arrows.
Why is Math.max(scores) NaN but Math.max(...scores) works?
Math.max([3, 9]) converts the array to a number ("3,9" → NaN). Math.max(...[3, 9]) is Math.max(3, 9).
What is [..."ok"]?
Strings are iterable by character (code point), so spreading one into an array gives its characters.
What does { name, ...(isAdmin && { role: "admin" }) } produce when isAdmin is false?
Spreading a primitive such as false, 0 or undefined into an object adds nothing, which makes this a compact way to add optional properties.
Which properties does { ...obj } copy?
Inherited properties (such as methods on a class prototype) and non-enumerable properties are not copied – which is why a spread class instance loses its methods.
Timed online tests with instant scores and explanations.