Question Bank

JavaScript Spread Operator Refactor: Two Explanations Quiz

Difficulty: Easy

Refactor a manual `sum(nums[0], nums[1], nums[2])` call with the spread operator, two equivalent shapes (spread shortcut and the older `apply(null, nums)` form), plus companions on rest parameters and array-spread vs concat.

Question Bank
/

JavaScript Spread Operator Refactor: Two Explanations Quiz

JavaScript Spread Operator Refactor: Two Explanations Quiz

Refactor a manual `sum(nums[0], nums[1], nums[2])` call with the spread operator, two equivalent shapes (spread shortcut and the older `apply(null, nums)` form), plus companions on rest parameters and array-spread vs concat.

Question Bank
Easy
JavaScript
4 questions
quiz
js-spread-rest
arrays
fundamentals

263 views

1

Refactor the snippet below so the call passes the array via the spread operator (...nums) instead of indexing each element manually.

Examples

Example 1:

Input:
const sum = (a, b, c) => a + b + c;
const nums = [4, 5, 6];
sum(nums[0], nums[1], nums[2]);
Output: 15
Explanation: spread expands the array into positional arguments at call time.