When using two equals signs for JavaScript equality testing, some funky conversions take place.
true | |||||||||||||||||||||
false | |||||||||||||||||||||
1 | |||||||||||||||||||||
0 | |||||||||||||||||||||
-1 | |||||||||||||||||||||
"true" | |||||||||||||||||||||
"false" | |||||||||||||||||||||
"1" | |||||||||||||||||||||
"0" | |||||||||||||||||||||
"-1" | |||||||||||||||||||||
"" | |||||||||||||||||||||
null | |||||||||||||||||||||
undefined | |||||||||||||||||||||
Infinity | |||||||||||||||||||||
-Infinity | |||||||||||||||||||||
[] | |||||||||||||||||||||
{} | |||||||||||||||||||||
[[]] | |||||||||||||||||||||
[0] | |||||||||||||||||||||
[1] | |||||||||||||||||||||
NaN |
When using three equals signs for JavaScript equality testing, everything is as is. Nothing gets converted before being evaluated.
true | |||||||||||||||||||||
false | |||||||||||||||||||||
1 | |||||||||||||||||||||
0 | |||||||||||||||||||||
-1 | |||||||||||||||||||||
"true" | |||||||||||||||||||||
"false" | |||||||||||||||||||||
"1" | |||||||||||||||||||||
"0" | |||||||||||||||||||||
"-1" | |||||||||||||||||||||
"" | |||||||||||||||||||||
null | |||||||||||||||||||||
undefined | |||||||||||||||||||||
Infinity | |||||||||||||||||||||
-Infinity | |||||||||||||||||||||
[] | |||||||||||||||||||||
{} | |||||||||||||||||||||
[[]] | |||||||||||||||||||||
[0] | |||||||||||||||||||||
[1] | |||||||||||||||||||||
NaN |
Note: This row does not match up with any of the rows in the other table.
true | if (true) { /* executes */ } | |
false | if (false) { /* does not execute */ } | |
1 | if (1) { /* executes */ } | |
0 | if (0) { /* does not execute */ } | |
-1 | if (-1) { /* executes */ } | |
"true" | if ("true") { /* executes */ } | |
"false" | if ("false") { /* executes */ } | |
"1" | if ("1") { /* executes */ } | |
"0" | if ("0") { /* executes */ } | |
"-1" | if ("-1") { /* executes */ } | |
"" | if ("") { /* does not execute */ } | |
null | if (null) { /* does not execute */ } | |
undefined | if (undefined) { /* does not execute */ } | |
Infinity | if (Infinity) { /* executes */ } | |
-Infinity | if (-Infinity) { /* executes */ } | |
[] | if ([]) { /* executes */ } | |
{} | if ({}) { /* executes */ } | |
[[]] | if ([[]]) { /* executes */ } | |
[0] | if ([0]) { /* executes */ } | |
[1] | if ([1]) { /* executes */ } | |
NaN | if (NaN) { /* does not execute */ } |
Always use 3 equals unless you have a good reason to use 2.