Possible Duplicate:
What is the !! (not not) operator in JavaScript?
What does the !! operator (double exclamation point) mean in JavaScript?
So I was debuging some code and ran across this:
var foo.bar = 0; // this is actually passed from another function, adding it for context
function(foo) {
var someVar = !!foo.bar;
if (foo.bar) {
// ..stuff happens
} else {
// .. something else happens
}
}
Okay my questions is what is the point of !!? All that is doing is making the 0 === false.
-
Is there any benefit to using that compared to
boolean(foo.bar)? -
foo.bar can be evaluated in an if as is because
0 === falsealready, so why go through the conversion? (someVar is not reused anywhere else)
This converts a value to a boolean and ensures a boolean type.
If
foo.baris passed through, then it may not be 0 but some other falsy value. See the following truth table:Truth Table for javascript
Javascript also gets really weird when it comes to NaN values. And this is the only case I can think of off the top of my head where !! would behave differently to ===.