Why does JavaScript evaluate plus with a string and integers differently depending on the place of the string?
An example:
console.log("1" + 2 + 3);
console.log(2 + 5 + "8");
The first line prints 123 and the second prints 78.
Why does JavaScript evaluate plus with a string and integers differently depending on the place of the string?
An example:
console.log("1" + 2 + 3);
console.log(2 + 5 + "8");
The first line prints 123 and the second prints 78.
Share Improve this question edited Jul 15, 2015 at 20:27 nirgn asked Jul 15, 2015 at 7:25 nirgnnirgn 2,0744 gold badges24 silver badges30 bronze badges 5 |5 Answers
Reset to default 21- JavaScript does automatic type conversion
The expression is evaluated left to right and therefore:
"1" + 2 + 3 -> "12" + 3 -> "123" 2 + 5 + "8" -> 7 + "8" -> "78"
JavaScript's type conversion in the cases you mention infers that you're looking to convert the types of all of your arguments to match the type of your first argument to the +
operator.
This is why in the first case you mention that starts with the string "1"
the addition converts the other arguments to strings.
In the second case you mentioned console.log(2 + 5 + "8")
. The first argument to the +
operator is an integer which is why JavaScript's type conversion assumes you want an integer.
Well, that's because the +
operator is overloaded.
- When used with two integers, it sums them up.
- When used with two strings, it concatenates them.
- When used between a string and an integer, it concatenates them.
That's why, when you do
console.log("1" + 2 + 3);
It concatenates the first and second operand (since the first one is a string) to give a string, "12", and it again concatenates it with the third operand for the same reason.
However, when you do
console.log(2 + 5 + "8");
It sums the first and second operands (both being numeric) to give 7
and finally concatenates it with the third operand for reasons mentioned above.
The expression evaluates from left to right.
So if you use "1" + 2 + 3
and the types of operands are different, it will append, otherwise if the types are ints it will add.
case "1" + 2 + 3
"1" +2 - different types- result - "12" string
"12" + 3 - different types- result - "123" string
case 2 + 5 + "8"
2 + 5 - same types- result - 7 int
7 + "8" - different types - result "78" string
I think it's called duck casting. If it looks like a duck, and quacks like a duck it must be a duck. If it looks like a string "1" then it's probably a string.
JavaScript also seems to evaluate both from left to right and from specific(int) to general (string).
In your first example, console.log("1" + 2 + 3), it's evaluating the "1" as a string because of the quotes and assuming you want to concatenate everything following it. It writes "123" as the result.
In the second example, console.log(2 + 5 + "8"), it takes an int and adds to another int producing 7, then encounters a string, so it assumes you want to do a concatenation so it writes "78" as a string. If you wanted to evaluate strictly int values, you could use parseInt("8") to convert your 8 back into an int.
+
is both the addition and concatenation operand, and which one is applied depends on the first value. – Matteo Tassinari Commented Jul 15, 2015 at 7:26