Number to string conversion clarification

This commit is contained in:
Brent Guffens 2018-01-17 11:32:49 +01:00 committed by GitHub
parent 2fb940557d
commit 2c3b1a34e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -43,7 +43,7 @@ let s = "my" + "string";
alert(s); // mystring alert(s); // mystring
``` ```
Note that if any of operands is a string, then the other one is converted to a string too. Note that if any of the operands is a string, then the other one is converted to a string too.
For example: For example:
@ -54,6 +54,13 @@ alert( 2 + '1' ); // "21"
See, it doesn't matter whether the first operand is a string or the second one. The rule is simple: if either operand is a string, then convert the other one into a string as well. See, it doesn't matter whether the first operand is a string or the second one. The rule is simple: if either operand is a string, then convert the other one into a string as well.
However, note that operations run from left to right. If there are two numbers followed by a string, the numbers will be added before being converted to a string:
```js run
alert(2 + 2 + '1' ); // "41" and not "221"
```
String concatenation and conversion is a special feature of the binary plus `+`. Other arithmetic operators work only with numbers. They always convert their operands to numbers. String concatenation and conversion is a special feature of the binary plus `+`. Other arithmetic operators work only with numbers. They always convert their operands to numbers.
For instance, subtraction and division: For instance, subtraction and division: