I find it puzzling how an empty array or an array with just one "numerical" value can be used in various calculations.
[] * [] === 0 // true
[2] * [2] === 4 // true
["2"] * ["2"] === 4 // true
Interestingly, not every operator behaves in the same way.
[2] + [1] === 3 // false, actual result is "21"
[2] - [1] === 1 // true
I decided to experiment with other comparisons, and here's what I found:
[4] === "4" // false
[4] === 4 // false
Normally, one would expect NaN
in such arithmetic operations. I wonder if JavaScript's array behavior is due to some internal utilization of the toString
method?