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
2019-11-23 23:37:11 -08:00
const greeting = (name = "Anonymous") => "Hello " + name;
2019-05-17 06:20:30 -07:00
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);
2019-11-27 02:57:38 -08:00
- text: A default parameter value of < code > 1</ code > should be used for < code > value</ code > .
2019-03-25 19:49:34 +05:30
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
2020-03-08 07:46:28 -07:00
// Only change code below this line
2019-03-25 19:49:34 +05:30
const increment = (number, value) => number + value;
2020-03-08 07:46:28 -07:00
// Only change code above this line
2018-09-30 23:01:58 +01:00
```
< / 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 >