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.
Written by Emmanuel Bourgerie
Related protips
1 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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Js
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#