Forum Moderators: open
function foo(bar='whatever') {
console.log('bar = ' + bar);
}
foo('example');
# bar = example
foo()
# bar = whatever function foo(bar) {
if (!bar) bar = 'whatever';
console.log(bar);
}
// or, I guess
function foo(bar) {
bar = bar || 'whatever';
console.log(bar);
} I'm not sure when I started doing that, but... I began using PHP over Perl sometime around 2008. I'm guessing that I saw it in PHP, tried it in Javascript, and it worked, so I've probably been doing it since 2008 or 09! But I'm pretty sure that I set most of the variables to false instead of 'whatever' so that I wouldn't have to check for "undefined".