Files

40 lines
1.2 KiB
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Use the Rest Parameter with Function Parameters
2018-10-12 15:37:13 -04:00
---
# Use the Rest Parameter with Function Parameters
2018-10-12 15:37:13 -04:00
#### Relevant LInks
2018-10-12 15:37:13 -04:00
- [Rest parameter explanation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters "Mozilla Developer Network")
2018-10-12 15:37:13 -04:00
- [Spread operator compared to rest parameter](https://stackoverflow.com/questions/33898512/spread-operator-vs-rest-parameter-in-es2015-es6 "Stack Overflow")
2018-10-12 15:37:13 -04:00
- <a href="http://www.youtube.com/watch?feature=player_embedded&v=iLx4ma8ZqvQ
2018-10-12 15:37:13 -04:00
" target="_blank"><img src="http://img.youtube.com/vi/iLx4ma8ZqvQ/0.jpg"
alt="Image of youtube video link spread and rest parameter " width="240" height="180" border="10" />Video explaining spread and rest</a>
2018-10-12 15:37:13 -04:00
### Example
This code
```javascript
const product = (function() {
"use strict";
return function product(n1, n2, n3) {
const args = [n1, n2, n3];
return args.reduce((a, b) => a * b, 1);
};
2018-10-12 15:37:13 -04:00
})();
console.log(product(2, 4, 6)); //48
2018-10-12 15:37:13 -04:00
```
Can be written as such
```javascript
const product = (function() {
"use strict";
return function product(...n) {
return n.reduce((a, b) => a * b, 1);
};
2018-10-12 15:37:13 -04:00
})();
console.log(product(2, 4, 6)); //48
2018-10-12 15:37:13 -04:00
```