Skip to content

Commit 1702112

Browse files
committed
operators
1 parent fa4c9be commit 1702112

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

1-js/2-first-steps/8-operators/article.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,50 +70,53 @@ alert( 2 - '1' ); // 1
7070
alert( 6 / '2' ); // 3
7171
```
7272

73+
## Numeric conversion, unary +
7374

75+
The unary plus or, in other words, the plus applied to a single value, doesn't do anything with numbers:
7476

75-
### Преобразование к числу, унарный плюс +
76-
77-
Унарный, то есть применённый к одному значению, плюс ничего не делает с числами:
7877

7978
```js
8079
//+ 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
8485
```
8586

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.
8790

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.
8992

90-
Например, когда мы получаем значения из HTML-полей или от пользователя, то они обычно в форме строк.
93+
What if we need to summarize them?
9194

92-
А что, если их нужно, к примеру, сложить? Бинарный плюс сложит их как строки:
95+
The binary plus would add them as strings:
9396

9497
```js
9598
//+ run
9699
var apples = "2";
97100
var oranges = "3";
98101

99-
alert( apples + oranges ); // "23", так как бинарный плюс складывает строки
102+
alert( apples + oranges ); // "23", the binary plus concatenates strings
100103
```
101104

102-
Поэтому используем унарный плюс, чтобы преобразовать к числу:
105+
So we can use the unary plus to convert values to numbers, and then sum them:
103106

104107
```js
105108
//+ run
106109
var apples = "2";
107110
var oranges = "3";
108111

109-
alert( +apples + +oranges ); // 5, число, оба операнда предварительно преобразованы в числа
112+
alert( +apples + +oranges ); // 5, both operands converted to numbers before the binary plus
110113
```
111114

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.
113116

114-
Почему унарные плюсы выполнились до бинарного сложения? Как мы сейчас увидим, дело в их приоритете.
117+
Why did unary pluses work before the binary one? As we're going to see soon, that's because of their *precedence*.
115118

116-
## Приоритет
119+
## Operators precedence
117120

118121

119122
В том случае, если в выражении есть несколько операторов -- порядок их выполнения определяется *приоритетом*.

0 commit comments

Comments
 (0)