DEV Community

Discussion on: The Semantics of Falsy Values

Collapse
 
yuanhao profile image
YuanHao Chiang • Edited

Great read!

function isOddNumber(num) { return num % 2; }
function isOddNumber(num) { return Boolean(num % 2); }

In my case, I find that:

function isOddNumber(num) { return (num % 2 !== 0); }

Is even more clear than an explicit Boolean conversion. I'm also not a big fan of !!value, as when codebase grows, it becomes hard to skimp through the code.

Cheers! :)

Collapse
 
somedood profile image
Basti Ortiz

Yes, that definitely works, too! However, I believe you meant num % 2 !== 0, though, since we're checking if a number is odd.

Either way, I get your point. That's a valid way of thinking about it rather than the explicit and rather bulky Boolean call.

Collapse
 
yuanhao profile image
YuanHao Chiang

Ooops, yes, edited my post :)