2018-10-12 15:37:13 -04:00
---
title: Set Default Parameters for Your Functions
---
## Set Default Parameters for Your Functions
2019-03-29 20:52:56 +01:00
Remember to use Read-Search-Ask if you get stuck. Try to pair program and write your own code.
2018-10-12 15:37:13 -04:00
2019-03-29 20:52:56 +01:00
### Problem Explanation:
2018-10-12 15:37:13 -04:00
```javascript
const increment = (function() {
"use strict";
return function increment(number, value) {
return number + value;
};
})();
console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns NaN
```
We'll be modifying the increment function so that the **number** parameter is incremented by 1 by default, by setting **value** to 1 if a value for **value** is not passed to the increment function.
2019-03-29 20:52:56 +01:00
### Hint: 1
2018-10-12 15:37:13 -04:00
Let's identify where the parameter **value** is in JS function
try to solve the problem now
2019-03-29 20:52:56 +01:00
### Hint: 2
2018-10-12 15:37:13 -04:00
Set **value** equal to something so that it is that value by default
try to solve the problem now
### Spoiler Alert!
Solution ahead!
2019-03-29 20:52:56 +01:00
## Basic Code Solution:
2018-10-12 15:37:13 -04:00
```javascript
const increment = (function() {
"use strict";
return function increment(number, value = 1) {
return number + value;
};
})();
console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns NaN
```
2019-03-29 20:52:56 +01:00
[Run Code ](https://repl.it/@RyanPisuena/PleasingFumblingThings )
2018-10-12 15:37:13 -04:00
2019-03-29 20:52:56 +01:00
### Code Explanation
2018-10-12 15:37:13 -04:00
2019-03-29 20:52:56 +01:00
* This section is pretty straightforward. Pass this section by setting the **value** parameter equal to 1. When the function comes across test cases where **value** has not been passed anything, then **value** will be assigned one by default.
2018-10-12 15:37:13 -04:00
2019-03-29 20:52:56 +01:00
Relevant Links:
2018-10-12 15:37:13 -04:00
2019-03-28 09:35:41 +01:00
[JavaScript default parameters ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters )
2018-10-12 15:37:13 -04:00
2019-03-29 20:52:56 +01:00
## NOTES FOR CONTRIBUTIONS:
2018-10-12 15:37:13 -04:00
2019-03-29 20:52:56 +01:00
* DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
2018-10-12 15:37:13 -04:00
* Add an explanation of your solution.
2019-03-29 20:52:56 +01:00
* Categorize the solution in one of the following categories — Basic, Intermediate and Advanced.