How assign default value to a param in a Javascript function: Basic
Defining a function
In this case true
it's the default value.
function myfunction(param) {
param = (param === undefined) ? true : param;
return param;
}
Obviously you can define your own custom value.
Testing in console
For the first example the default value output is true
> myfunction();
true
> myfunction("Hello world !");
"Hello world !"
> myfunction("");
""
> myfunction(123456);
123456
> myfunction(null);
null
> myfunction(false);
false
> myfunction(true);
true
Written by Jose Luis Quintana
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Default
Authors
dpashkevich
125.5K
eladmeidar
2.278K
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#