In PHP I can do something like this:
public myFunction($variable=0) {
//Do some stuff
}
In Javascript, the following does not work:
var myFunction = function(variable=0 )
{
//do some stuff...
};
In PHP I can do something like this:
public myFunction($variable=0) {
//Do some stuff
}
In Javascript, the following does not work:
var myFunction = function(variable=0 )
{
//do some stuff...
};
Try this
var myFunction = function(variable) {
variable = variable || 0;
// this variant works for all falsy values "", false, null, undefined...
// if you need check only undefiend, use this variant
// variable = typeof(variable) == 'undefined' ? 0 : variable
};
Default function parameters will be in ES6; ES5 does not allow it.
With ECMAScript 6 you can do this.
Check an example:
function multiply(a, b = 1) {
return a*b;
}
multiply(5); // 5
But this is still experimental and today is only supported by Mozilla Firefox browser.
Check this link do see documentantion: