function greeting(name = "Anonymous") {The default parameter kicks in when the argument is not specified (it is undefined). As you can see in the example above, the parameter
return "Hello " + name;
}
console.log(greeting("John")); // Hello John
console.log(greeting()); // Hello Anonymous
name
will receive its default value "Anonymous"
when you do not provide a value for the parameter. You can add default values for as many parameters as you want.
increment
by adding default parameters so that it will add 1 to number
if value
is not specified.
increment(5, 2)
should be 7
.'
testString: 'assert(increment(5, 2) === 7, ''The result of increment(5, 2)
should be 7
.'');'
- text: The result of increment(5)
should be 6
.
testString: 'assert(increment(5) === 6, ''The result of increment(5)
should be 6
.'');'
- text: default parameter 1
was used for value
.
testString: 'getUserInput => assert(getUserInput(''index'').match(/value\s*=\s*1/g), ''default parameter 1
was used for value
.'');'
```