Variadic functions
Accepting a variable number of values in legacy or interop code.
JS Puzzles · Const
The arguments object is an array-like, iterable collection of every value supplied to a traditional function. It supports indexes and length, but not Array methods, and arrow functions inherit arguments from an enclosing normal function.
Inside a traditional function, arguments exposes every value supplied at the call site through numeric indexes and length. It is iterable and array-like, but it is not an Array, so convert it before using array methods. Arrow functions do not create their own arguments object.
argumentsfunction total() {
return Array.from(arguments).reduce((sum, value) => sum + value, 0);
}
console.log(total(2, 3, 5)); // 10Converts the array-like object to an array, then sums every supplied argument.
Accepting a variable number of values in legacy or interop code.
Inspecting how many values a caller supplied.
Iterating over caller-supplied values in a traditional function.
rest parametersspread syntaxArray.from()for...of