How do I tell Javascript to use zero as a variable if the passed variable if empty? In PHP I can use the following.
<?php
function test($var=0){
echo $var;
}
?>
So it would set $var to 0 if the passed variable was empty. I can’t seem to figure this out in Javascript.
Thanks
If you call a method without providing a value for one (or more) of the arguments, those arguments will be
undefined.Thus within the method you can test whether the variable is defined, and if not assign it a default value:
It’s not very elegant, but Javascript doesn’t have a nice native default-arguments syntax.