Function arguments without assign are undefined
which is a falsy
value. So you can write:
function myFunc(trueOrFalse) {
return trueOrFalse
}
Calling myFunc()
will return undefined
which is falsy
. Just Beware that falsy
is not equals to false
:
if(false) {
// it will not be executed
}
if(undefined){
// it will not be executed
}
// but:
false == undefined // false
So depending your needs you'd want transform this to false
, one approach could be using default parameters:
function myFunc(trueOrFalse = false) {
return trueOrFalse
}
calling myFunc()
now will return false
, for example.