JS Built-ins
← Array16 / 226
ArrayArray.fromadvanced

Array.from() β€” sum variadic arguments

πŸ“– Usage Example
function sum() {
  return Array.from(arguments).reduce((a, b) => a + b, 0)
}
sum(1, 2, 3)  // β†’ 6

Use Array.from(arguments) to convert the arguments object into a real array.

Array.from() β€” converting arguments

The legacy arguments object inside a function is array-like (has length and integer keys) but is not a real array. Array.from(arguments) converts it into one so you can use array methods.

With modern TypeScript you'd use rest parameters (...args), but knowing how to convert arguments is useful when working with older code.

Challenge: Implement sumAll that accepts any number of numbers and returns their sum. Use Array.from inside the implementation.

sumAll(1, 2, 3)     // β†’ 6
sumAll(10, 20)      // β†’ 30
πŸ’‘ Hints (2)
  • β€’ Rest parameters (`...args`) are already a real array β€” `Array.from(args)` produces a copy of it.
  • β€’ Chain `.reduce((acc, n) => acc + n, 0)` on the result to sum all values.