Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[] == ![] evaluates to true

I would like to know why the expression given in the title

[] == ![]

is evaluated to true.

You cannot compare arrays as strings. I get that. If

[] == []

will evaluate to false because the references are different. Though if we have the following statement.

var arr = []; 
arr == arr // this evaluates to true simply because references are the same.

In order A == B to return true either A and B have to be false or true. A == !B in order to return true A can be true and B can be false or vice versa but in this case, A and B are the same values so I don't get it.

like image 252
Tek Avatar asked Jul 29 '16 15:07

Tek


People also ask

Why [] == ![] Is true in JS?

Double equals, == , performs an amount of type coercion on values before attempting to check for equality. So arr == arr returns true as you'd expect as what you are actually checking is if [] == [] and both sides of the equation are of the same type.

What evaluates to true JavaScript?

In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy. That is, all values are truthy except false , 0 , -0 , 0n , "" , null , undefined , and NaN .

Can a value be both true and false JS?

For this, JavaScript has a Boolean data type. It can only take the values true or false.


3 Answers

Basically Javascript tries to convert both the sides into Number if both the types are not same. And if its an Object, It tries to convert into primitive value

So in this case step by step will be

=> []==![]

=> []==false // Type conversion by the statement itself

=> []==0 // To number of right operand

=> ""==0 // To Primitive call for Array which will in this case convert to empty string

=> 0==0 // To number call of "" which is 0

=> true

One can check for the ecmascript explanation here in the compiler description http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

like image 121
Akarsh Satija Avatar answered Oct 05 '22 00:10

Akarsh Satija


Whenever 2 values are compared using == , javascript performs the Abstract Equality Comparison Algorithm.

enter image description here

Here, x is [], and y is ![]. Also,

typeof([]) // "object"
typeof(![]) // "boolean"

Since y is a boolean and x is an object, condition 7 is the first to hold:

If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

What’s the value of ToNumber(y)?

Number(![]) // 0

because [] is a truthy value, negating makes it false. Number(false) is 0

Now we have the comparison: [] == 0.

Since typeof(0) is "number", condition 8 now holds:

If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y.

ToPrimitve(x) is like x.toString().

[].toString() // ”” - the empty string

Almost done we now face with the comparison: “” == 0

Now, condition 5 holds:

If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.

ToNumber(“”) // 0

Finally both operands have the same type and condition 1 holds. I think you can take if from here :)

read about Abstract Equality Comparison on the specs!

like image 32
Bar Horing Amir Avatar answered Oct 05 '22 00:10

Bar Horing Amir


![] evaluates to false because the reference is truthy. [] can be converted to a number ( 0 in this case ) which is a falsy value. Therefore: the condition passes as equal. If you did === it would be false.

like image 38
Daniel A. White Avatar answered Oct 05 '22 01:10

Daniel A. White