2

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?

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

But why does [''] == [''] and [''] === [''] return false?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.