Mureinik's answer is completely right, but seeing that “understanding false values” is one of the most important, less intuitive parts of JavaScript may be worth a little more explanation.
Without a second set of brackets, a statement that needs to be evaluated
actually rated as
(!a) > 0
So what does (! A) mean? This means finding the logical truth "a" and flipping it; true becomes false and false becomes true. The logical likelihood of "a" means - if a has one of the meanings that is considered "false", then it is false. In all other cases, i.e. For all other possible meanings of a, this is true. False values:
false 0 (and -0) "" (the empty string) null undefined NaN (Not a Number - a value which looks like a number, but cannot be evaluated as one
So, if a has any of these meanings, it is false and! a true If he has something else, it is true, and therefore! A is false.
And then we try to compare this with 0. And 0, as you know, can also be false, so your comparison is
if (true > false) {}
or
if (false > false) {}
Seeing that neither truth nor falsehood can ever be anything other than false (they cannot be more or less!), Your "if" will always fail, and the code inside the brackets will never be evaluated.
Ben green
source share