Skip to content

Commit 2c3b1a3

Browse files
authored
Number to string conversion clarification
1 parent 2fb9405 commit 2c3b1a3

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

1-js/02-first-steps/07-operators/article.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ let s = "my" + "string";
4343
alert(s); // mystring
4444
```
4545

46-
Note that if any of operands is a string, then the other one is converted to a string too.
46+
Note that if any of the operands is a string, then the other one is converted to a string too.
4747

4848
For example:
4949

@@ -54,6 +54,13 @@ alert( 2 + '1' ); // "21"
5454

5555
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.
5656
57+
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:
58+
59+
60+
```js run
61+
alert(2 + 2 + '1' ); // "41" and not "221"
62+
```
63+
5764
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.
5865
5966
For instance, subtraction and division:

0 commit comments

Comments
 (0)