Twisted logic: understanding truthy and falsy
One of these is the concept of truthy and falsy. These are sort of like true/false-lite, which will anger you somewhat if you majored in logic or philosophy. You mean you didn't know true and false were in fact gradable, not absolute concepts!?
1var someVar = 0;
2alert(someVar == false); //evaluates true
alert(false == false); //evaluates true, of course
alert(someVar === false); //evaluates false - someVar is a number, not a legitimate boolean
It gets worse
Clear enough? It gets more complex.
alert( == false); //evaluates true
How can this be, if an empty array is a truthy? (it is often mistakenly stated as being a falsy).
1alert( == true); //evaluates false
2alert(); //evaluates true - so an empty array is a truthy
Thus, the latter approach really shows what is and isn't a truthy/falsy.
This might sound new to you, but chances are you've experienced it before. The most common example is when you output an array.
alert(['one', 'two', 'three']); //alerts('one,two,three');
If you want to be strict in your comparisons and avoid data coercion you should use the comparison operator ===, which compares not only value but also type.
Hope you found this helpful.