views:

29

answers:

1

According to Firebug console, we have the following in JavaScript:

>>> [''] == ''
true
>>> [''] == ['']
false

Finding Python to be much more logical here, I'd expect it to be the way round. Anyway, I can understand the second one — apparently two different objects never compare equal to each other, — but what is the reason for the first to give true? What string would ['', ''] compare equal to?

+2  A: 

It's comparing the string representation of the array on the left to the string on the right.

alert(['', ''] == ','); // true

alert([1, 2] == '1,2'); // true

Of course you can use the strict comparison operator to avoid this...

alert([''] === ''); // false
no
Thanks. It's kind of stupid of JS designers in my opinion, but oh well...
doublep
But why does [''] == [''] and [''] === [''] return false?
qw3n
qw3n: because it's not the same object. Think about it like this: `var a=[''], b=['']; a[0]=123; a==b; //false` compared to `var a,b; a=b=['']; a[0]=123; a==b; //true`
no