Why is if ([]) checked and [] == false in javascript?

if([] == false) alert('empty array is false'); alert(+[]) // alert 0 if([]) alert('empty array is true'); 

Both of them will trigger an alert.

Demo

+14
javascript boolean
Dec 10 '12 at 10:52
source share
3 answers

Both current answers are correct here, but I would like to add a more detailed explanation based on the language specification . The reason for the clearly conflicting results is that the if and comparison comparisons are evaluated differently.

In the case of the if(expression) operator, the expression is evaluated and then converted to the Boolean type ( § 12.5 ). Arrays are objects, and when an object is converted to Boolean, the result is always true ( § 9.2 ).

Comparisons of equality with == follow another set of rules, described in detail in § 11.9.3 . For comparison, several type conversions may be required while both operands are of the same type. The order of the operands is also important. According to this algorithm, we can see that the comparison [] == false is actually a four-step operation:

  • A boolean is involved here, so it is first converted to a number (step 7 of the algorithm). So it is done:

     [] == 0 
  • Then the array is converted to its primitive value (see § 9.1 and § 8.12.8 ) and becomes an empty string (step 9). So:

     "" == 0 
  • When comparing a string with a number, the string is first converted to a number (step 5, following the rules described in § 9.3.1 ):

     0 == 0 
  • Now that we have two numbers, the comparison evaluates to true according to step 1.c.iii.
+18
Dec 10 '12 at 13:01
source share

This is due to the type of coercion of the == operator (equality).

An empty array is considered true (as an empty object), so a second warning is raised.

However, if you use ([] == false) , your array is forced to its string representation *, which is a "" , which is then considered a false value , which makes the condition true, thereby also triggering the first warning.

If you want to avoid type coercion, you should use the === (identity) operator, which is preferred, and the famous Douglas Crockford advanced comparison method in javascript.

You can read more about this in this comprehensive answer .

* ( Object.prototype.toString is called on it)

EDIT: Fun with JS Comparison:

 NaN == false // false NaN == true // also false NaN == NaN // false if(NaN) // false if(!NaN) // true 0 == '0' // true '' == 0 // true '' == '0' // false ! 

This shows the real "strength" of comparing with == due to the weird rules mentioned in the bfavarettos answer.

+18
Dec 10 '12 at 11:00
source share

There is a difference between evaluating a value as Boolean and matching it with true or false .

Using the == operator, values ​​are converted so that the corresponding types match. The value [] , converted to the empty string "" , and the conversion, which, in turn, to Boolean, gives false , so [] == false becomes true.

Evaluating [] as a boolean will return true because it is not a false value, that is, 0 , false , null , "" , NaN or undefined .

+3
Dec 10 '12 at 11:01
source share



All Articles