Last Updated: February 25, 2016
·
1.037K
· manudwarf

Safe boolean check

If you want to return true or false for a property that you are not sure te be present, here is a quick check :

console.log(my.property ? true : false);

In case of "undefined", will return false, otherwise the value.

1 Response
Add your response

my.property || false works as well and is slightly less verbose and has the benefit of actually returning the value of my.property if it isn't falsey without having to duplicate the variable name like my.property ? my.property : false

over 1 year ago ·