1

I were trying to play around the equal to == operator in Javascript, and I got the following results:

0 == "0" // true

and also

0 == [0] // true

HOWEVER:

"0" == [] // false

Honestly, this is kind of confusing for me since I have no Javascript-background.

Also, I noticed that:

"0" == [0] // true

and that is also applicable for other values:

1 == [1] // true
1 == "1" // true
"1" == [1] // true

101 == "101" // true
101 == [101] // true
"101" == [101] // true

So it seems to be about comparing 0 with an empty array [].

What is logic behind it?

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • 1
    Also [Which equals operator (== vs ===) should be used in JavaScript comparisons?](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) – Mohammad Usman Apr 14 '18 at 10:43
  • there are countless questions like this,https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons – xianshenglu Apr 14 '18 at 10:46

4 Answers4

2

If you see the Loose equality using == chart on Mozila Developer Network here at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness you can see that

If Operand A is string in your case "0" and Operand B is Object in your case [] then Operand B will be converted to primitive type like new String([]) returns ""

A == ToPrimitive(B)

Then if we try,

> typeof "0"
 "string"
> typeof []
 "object"

> "0"=="" // it will return false

If you try with

> 0==[]  // it will return true
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
1

It happens because of type coercion (using double =). Basically when comparing variables that are not of equal types, javascript tries and covert them to the same type and the compare them. To avoid this use triple equals (===) when comparing variables.

To know more about javascript I recommend these books (free online!)

https://github.com/getify/You-Dont-Know-JS

but if you're interested in this topic specifically you can read this one:

https://github.com/getify/You-Dont-Know-JS/blob/master/types%20%26%20grammar/ch4.md

PS. Down at the end is exactly your example explained in detail.

Good luck!

mihelcic
  • 56
  • 5
0

It is working based Abstract equality comparison algorithm. Let me pick a single example from yours and explain how it is returning the results,

0 == [0]

So the main aim of AECA is to bring the lhs and rhs to a same type. Based on the algorithm, internally recursive steps would happen to make both the sides of same type.

i]   0 == [0]
ii]  0 == toPrimitive([0])
iii] 0 == '0'
iv]  0 == toNumber('0')
v]   0 == 0
vi]  true
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
0

To put it simply, javascript will try most of the times to convert the sides of the equality to numbers if they are of different type. For object, it will however call the abstract operation toPrimitive (with a hint of default). That operation calls .valueOf and then .toString on the result if it's still not a primitive ( meaning something else than an object).

0 == "0" => 0 == 0 => true

0 == [0] => 0 == "0" ( .valueOf returns [0] so .toString is used)

"0" == [] => "0" == "" => false ( they have the same type so the string must be the same to be true)

0 == [] => 0 == "" => 0 == 0 : true

The full resource is here:

https://www.ecma-international.org/ecma-262/8.0/#sec-abstract-equality-comparison
Axnyff
  • 9,213
  • 4
  • 33
  • 37