You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/2-first-steps/8-operators/article.md
+19-16Lines changed: 19 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -70,50 +70,53 @@ alert( 2 - '1' ); // 1
70
70
alert( 6/'2' ); // 3
71
71
```
72
72
73
+
## Numeric conversion, unary +
73
74
75
+
The unary plus or, in other words, the plus applied to a single value, doesn't do anything with numbers:
74
76
75
-
### Преобразование к числу, унарный плюс +
76
-
77
-
Унарный, то есть применённый к одному значению, плюс ничего не делает с числами:
78
77
79
78
```js
80
79
//+ run
81
-
alert( +1 );
82
-
// 1
83
-
alert( +(1-2) ); // -1
80
+
var x =1;
81
+
alert( +x ); // 1
82
+
83
+
var y =-2;
84
+
alert( +y ); // -2
84
85
```
85
86
86
-
Как видно, плюс ничего не изменил в выражениях. Результат -- такой же, как и без него.
87
+
As we can see, the plus didn't change a thing. The result is the same as it was.
88
+
89
+
But there is a widely used "side-effect": if the plus is applied not not a number, it converts it to a number.
87
90
88
-
Тем не менее, он широко применяется, так как его "побочный эффект" -- преобразование значения в число.
91
+
For example, if we are getting values from HTML-fields or the visitor types them in a prompt, then they are usually strings.
89
92
90
-
Например, когда мы получаем значения из HTML-полей или от пользователя, то они обычно в форме строк.
93
+
What if we need to summarize them?
91
94
92
-
А что, если их нужно, к примеру, сложить? Бинарный плюс сложит их как строки:
95
+
The binary plus would add them as strings:
93
96
94
97
```js
95
98
//+ run
96
99
var apples ="2";
97
100
var oranges ="3";
98
101
99
-
alert( apples + oranges ); // "23", так как бинарный плюс складывает строки
102
+
alert( apples + oranges ); // "23", the binary plus concatenates strings
100
103
```
101
104
102
-
Поэтому используем унарный плюс, чтобы преобразовать к числу:
105
+
So we can use the unary plus to convert values to numbers, and then sum them:
103
106
104
107
```js
105
108
//+ run
106
109
var apples ="2";
107
110
var oranges ="3";
108
111
109
-
alert( +apples ++oranges ); // 5, число, оба операнда предварительно преобразованы в числа
112
+
alert( +apples ++oranges ); // 5, both operands converted to numbers before the binary plus
110
113
```
111
114
112
-
С точки зрения математики такое изобилие плюсов может показаться странным. С точки зрения программирования -- никаких разночтений: сначала выполнятся унарные плюсы, приведут строки к числам, а затем -- бинарный `'+'` их сложит.
115
+
From a mathematician's standpoint the abundance of pluses may seem strange. But from a programmer's -- there's nothing special: unary pluses are applied first, they convert strings to numbers, and then the binary plus summarizes them.
113
116
114
-
Почему унарные плюсы выполнились до бинарного сложения? Как мы сейчас увидим, дело в их приоритете.
117
+
Why did unary pluses work before the binary one? As we're going to see soon, that's because of their *precedence*.
115
118
116
-
## Приоритет
119
+
## Operators precedence
117
120
118
121
119
122
В том случае, если в выражении есть несколько операторов -- порядок их выполнения определяется *приоритетом*.
0 commit comments