Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why if([]) is validated while [] == false in javascript?

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

They both will run the alert

Demo

like image 504
Jonathan de M. Avatar asked Dec 10 '12 10:12

Jonathan de M.


People also ask

Why [] == [] is false in JavaScript?

The reason for [] == false even though [] is truthy is: the comparison [] == false compares the value of [] to false . And to get the value of [] , the JavaScript engine first calls []. toString() . That results in "" , and that is what's actually compared to false .

Why True == true is false in JS?

If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible.

How can you tell if JavaScript is true or false?

Use the strict equality (===) operator to check if a variable is equal to false - myVar === false . The strict equality operator will return true if the variable is equal to false , otherwise it will return false .

Does === return false?

Note that === never causes type coercion, but checks for correct types first and yields false if they are not equal!


2 Answers

Both current answers here are correct, but I'd like to add a more detalied explanation based on the language specification. The reason for the apparently contradictory outcomes is that if statements and equality comparisons are evaluated differently.

In the case of an if(expression) statement, 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).

Equality comparisons with == follow a different set of rules, detailed on § 11.9.3. The comparison may require multiple type conversions, until both operands are the same type. The order of the operands is also important. According to that algorithm, we can see that the comparison [] == false is actually a four-step operation:

  1. There is a Boolean involved, so it's converted to a Number first (step 7 of the algorithm). So it becomes:

    [] == 0
    
  2. 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
    
  3. When comparing a String to a Number, the String is converted to Number first (step 5, following the rules described on § 9.3.1):

    0 == 0
    
  4. Now that we have two Numbers, the comparison evaluates to true according to step 1.c.iii.
like image 159
bfavaretto Avatar answered Sep 22 '22 14:09

bfavaretto


It's because of type coercion of the == (equality) operator.

An empty array is considered truthy (just like an empty object), thus the second alert is called.

However, if you use ([] == false), your array is coerced to its string representation* which is "" which then is considered as a falsy value, which makes the condition true thus triggering the first alert too.

If you want to avoid type coercion, you have to use the === (identity) operator which is the preferred and by the famous Douglas Crockford promoted way to compare in javascript.

You can read more on that matter in this exhaustive 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 you the real "power" of Comparison with == due to the strange rules mentioned in bfavarettos answer.

like image 38
Christoph Avatar answered Sep 18 '22 14:09

Christoph