If you want to check that someBool
is in fact true
, use the strict equality comparator (===
):
var someBool = 1;
someBool == true; // true
someBool === true; // false
This also works for other types:
0 == "0"; // true
0 === "0"; // false
As others have pointed out, there is a difference between testing for equality (using ==
) or testing for "truthyness" (using just if()
). It's definitely worth knowing about this, but personally I try to avoid writing code that is not 100% clear in its meaning. Given the name of the variable (someBool
), I would expect a boolean, so I would test for a boolean. If I wanted to know if it was greater than 0
, I would test for that instead (someNumber >== 0
).
As for performance; the strict equality comparator is usually a bit faster than using ==
, but the difference is negligible. Testing for truthyness might be a bit faster, but this is still a micro-optimization, which is rarely worth the trouble.