Javascript - Rest Parameters
Code Life
By Pierre Marsaa
Gathering Arguments Into an Array
Imagine you want to write a function that heals your Codémon after a battle — sometimes 2 Codémon, sometimes 3, sometimes 6. Defining a fixed list of parameters quickly breaks down when you pass fewer arguments than expected:
↗️
Loading editor...
↶
↷
To let a function accept any number of arguments, write three dots (...) before the parameter name. This is called a rest parameter: it gathers all passed arguments into a single array:
↗️
Loading editor...
↶
↷
Verify