2018-09-30 23:01:58 +01:00
---
id: 587d7b88367417b2b2512b46
title: Set Default Parameters for Your Functions
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301209
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
In order to help us create more flexible functions, ES6 introduces < dfn > default parameters< / dfn > for functions.
Check out this code:
2019-05-17 06:20:30 -07:00
```js
function greeting(name = "Anonymous") {
return "Hello " + name;
}
console.log(greeting("John")); // Hello John
console.log(greeting()); // Hello Anonymous
```
2018-09-30 23:01:58 +01:00
The default parameter kicks in when the argument is not specified (it is undefined). As you can see in the example above, the parameter < code > name< / code > will receive its default value < code > "Anonymous"< / code > when you do not provide a value for the parameter. You can add default values for as many parameters as you want.
< / section >
## Instructions
< section id = 'instructions' >
Modify the function < code > increment< / code > by adding default parameters so that it will add 1 to < code > number< / code > if < code > value< / code > is not specified.
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2018-10-20 21:02:47 +03:00
- text: The result of < code > increment(5, 2)</ code > should be < code > 7</ code > .
2019-03-25 19:49:34 +05:30
testString: assert(increment(5, 2) === 7);
2018-10-04 14:37:37 +01:00
- text: The result of < code > increment(5)</ code > should be < code > 6</ code > .
2019-03-25 19:49:34 +05:30
testString: assert(increment(5) === 6);
- text: Default parameter < code > 1</ code > was used for < code > value</ code > .
testString: assert(code.match(/value\s*=\s*1/g));
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
2019-03-25 19:49:34 +05:30
const increment = (number, value) => number + value;
2018-09-30 23:01:58 +01:00
console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns 6
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-03-25 19:49:34 +05:30
const increment = (number, value = 1) => number + value;
2018-09-30 23:01:58 +01:00
```
2019-07-18 08:24:12 -07:00
2018-09-30 23:01:58 +01:00
< / section >