I am writing a Javascript function with an optional argument, and I want to assign the optional argument a default value. How can I assign it a default value?
I thought it would be this, but it doesn't work:
function(nodeBox,str = "hai")
{
// ...
}
This question is related to
javascript
You can also do this with ArgueJS:
function (){
arguments = __({nodebox: undefined, str: [String: "hai"]})
// and now on, you can access your arguments by
// arguments.nodebox and arguments.str
}
ES6 Update - ES6 (ES2015 specification) allows for default parameters
The following will work just fine in an ES6 (ES015) environment...
function(nodeBox, str="hai")
{
// ...
}
Source: Stackoverflow.com