# How to accept unlimited parameters in a JavaScript function

> Learn how to write a JavaScript function that accepts an unlimited number of arguments, using rest parameters that collect them into a single array.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2020-06-22 | Topics: [JavaScript](https://flaviocopes.com/tags/js/) | Canonical: https://flaviocopes.com/how-to-unlimited-function-parameters-js/

Let's say we have a function called `join()` whose job is to join all the strings we pass to it.

For example we write a prototype that accepts 2 strings:

```js
const join = (string1, string2) => {
  return string1 + string2
}
```

and when we call it, we get a string that is the concatenation the 2 arguments we pass:

```js
join('hi', ' flavio') // 'hi flavio'
```

One simple way is to append additional parameters that default to an empty string, like this:

```js
const join = (string1, string2, string3 = '') => {
  return string1 + string2 + string3
}
```

but this approach does not scale well, because we'd need to add a large number of parameters and our code would look pretty bad.

Instead, we can use this syntax, with the spread operator (`...`) followed by the name of the parameter we want to use. Inside the function, the parameter is an array, so we can simply call its `.join()` method to concatenate the strings it contains, passing an empty string as argument (otherwise it defaults to concatenate strings adding a comma between them):

```js
const join = (...strings) => {
  return strings.join('')
}
```

In our case we can also simplify this using the implicit return syntax available in arrow functions:

```js
const join = (...strings) => strings.join('')
```

and we can call this in the same way we did before:

```js
join('hi', ' flavio') // 'hi flavio'
join('hi', ' flavio', ' it', ' is', ' a', ' beautiful day!') // ''hi flavio it is a beautiful day!'
```
