'1' == 1
'1' == [1]
'1' == true
1 == [1]
1 == true
[1] == true
But this is false: [1] == [1][1] == 1 and 1 == [1], so it should be the case that [1] == [1].
Just because it's what happens when you apply the rules of the language doesn't mean it actually makes sense.
It is funny that you are asking why would [1] == [1] possibly considered to be true.
Let's see in a language with a sane and consistent typing system like Python:
In [1]: [1]==[1]
Out[1]: True
Ok, let's do a crazy language from Sweden also with dynamic types but which are sane and consistent, like Erlang: 1> [1] == [1].
true
Surely it will be broken in Ruby: irb(main):001:0> [1] == [1]
=> true
Nah clearly it should be false, these crazy languages just haven't heard about objects and identities and such.Here's another example:
4 == 4 // true
a = new Number(4);
b = new Number(4);
a == b // false
a == a // true
a.valueOf() // 4
b.valueOf() // 4
a.valueOf() == b.valueOf() // true
And if this isn't clear yet: a = {}
b = {}
a == a // true
a == b // falseSo I gave a couple of examples from other dynamic languages where the the sane behavior is "obvious".
I am not being a dick I am saying the language is broken. Explaining the historical context or the internal implementation of it doesn't make it unbroken. Like one can explain why COBOL uses this construct or that and how it came to be, doesn't make COBOL better or more appealing. One of course might not have a choice and be stuck using it but lying to oneself about how awesome it is, is not necessary.
> The other languages you cited do not work that way.
The don't, and I like how they work better.
Now whether one has a choice to use or not use JS is a different topic. Usually there is no choice on the client side. But somehow extolling Javascript typing rules as being sane, making sense, or as someone below put it "brilliant" is a bit silly.
This is because you require the programmer to always understand the output (and defaults) of every call made. Sometimes it's undefined. Sometimes it's null. Sometimes it's empty string. This takes experience.
Example, the default for getAttribute is null, but an undefined property is undefined:
document.body.getAttribute("test"); // null
document.body.test; // undefined
Except if it's a special property, then getAttribute might still be null, but the property empty string: var input = document.createElement("input");
input.getAttribute("value"); // null
input.value; // ""
Remember, "" !== null and null !== undefined. 5 + (10 * [2]) + (9 + [1/'']) // 259Infinity
5 ~+ (10 ~* [2]) ~+ (9 ~+ [1 ~/ '']) // throw exception