Variadic Functions in JavaScript
Variadic functions can be called with any number of trailing arguments. For example, console.log
is a common variadic function.
Here’s a function that will take an arbitrary number of number
s as arguments.
Here’s a function that will take an arbitrary number of number
s as arguments. Within the function, the type of nums
is equivalent to number[]
. We can get its length, iterate over it using loops, etc.
Variadic functions can be called in the usual way with individual arguments.
If you already have multiple args in an array, apply them to a variadic function using func(...array)
like this.
Now that we can use variadic functions in JavaScript, let’s learn more about the language.