DEV Community

Discussion on: 2 Ways to Convert Values to Boolean in JavaScript

Collapse
 
wrldwzrd89 profile image
Eric Ahnell

Well that’s neat! I didn’t realize that an expression shorthand existed for conversion to Boolean. Thanks for posting!

Collapse
 
jonrandy profile image
Jon Randy 🎖️

It isn't a shorthand, it is part of how the language works. An 'if' statement expects a boolean, so it will coerce whatever you give it to a boolean - no need to do the conversion yourself

Collapse
 
samanthaming profile image
Samantha Ming

Exactly Jon! I remember being told auto coercion is this scary beast. But then if you understand it, you can make the beast work for you 😊. It's just another thing JavaScript tries to help. And if you know what it's trying to do, why not let it 😉

Collapse
 
wrldwzrd89 profile image
Eric Ahnell

Yes, after thinking about it, I realized that you’re coercing to Boolean and negating, then undoing negation. My “shorthand” remark was meant in the sense of fewer characters to type.