The JS spec seems to say two things:
- When a number and a string are added, the number is coerced into a string, and the two strings are concatenated.
"2" + 7 == "27"
and2 + "7" == "27"
are both true. - The
+
operator is right-associative.
If +
is right-associative, then I would expect chained addition operations like 4 + 3 + 2 + "1"
to be evaluated like 4 + (3 + (2 + "1"))
, and for the logic to be
2 + "1"
evaluates to"21"
3 + "21"
evaluates to"321"
4 + "321" evaluates to "4321"
But instead, the expression evaluates to "91"
, like it's doing 4 + 3 = 7
, 7 + 2 = 9
, 9 + "1" = "91"
.
What have I misunderstood or missed?