It breaks down like this:
Starting with:
[1,2] + [4,5,6][1]
First, each side gets evaluated, and since the right-hand side is an array initializer and lookup, it comes out to 5
:
[1,2] + 5
Now the + operator starts its work. It isn't defined for arrays, the first thing it does is try to convert its operands into either strings or numbers. In the case of an array, it'll be a string as though from Array#toString
, which does Array#join
, giving us:
"1,2" + 5
When you use +
where either side is a string, the result is string concatenation.