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
Присвоєння — це простий випадок. Можливі побічні ефекти, які не з'являтимуться, якщо обчислення не досяген їх.
147
147
148
-
As we can see, such a use case is a "shorter way of doing `if`". The first operand is converted to boolean. If it'sfalse, the second one is evaluated.
148
+
Як ми бачимо, таке використання є "більш коротким способом виконання `if`". Перший операнд перетворюється на булевий. Якщо він false, то обчислюється другий.
149
149
150
-
Most of time, it's better to use a "regular" `if` to keep the code easy to understand, but sometimes this can be handy.
150
+
У більшості випадків краще використовувати "звичайний" `if`, щоб код буле легше зрозуміти, але іноді це може бути зручно.
151
151
152
152
## && (AND)
153
153
154
-
The AND operator is represented with two ampersands `&&`:
154
+
Оператор AND представлений двома амперсандами `&&`:
155
155
156
156
```js
157
157
result = a && b;
158
158
```
159
159
160
-
In classical programming, AND returns `true` if both operands are truthy and `false` otherwise:
160
+
У класичному програмуванні AND повертає `true`, якщо обидва оператора є правдивими і `false` у іншому випадку:
The precedence of AND `&&` operator is higher than OR `||`.
233
+
````smart header="Приорітет AND `&&` є більш високим за OR `||`"
234
+
Приорітет оператора AND`&&`є більш високим заOR`||`.
235
235
236
-
So the code `a && b || c && d` is essentially the same as if the `&&` expressions were in parentheses: `(a && b) || (c && d)`.
236
+
Отже, код `a && b || c && d`по суті є таким самим, як би вираз `&&`був у дужках:`(a && b) || (c && d)`.
237
237
````
238
238
239
-
Just like OR, the AND `&&` operator can sometimes replace `if`.
239
+
Так само, як OR, операторAND`&&`може іноді заміняти`if`.
240
240
241
-
For instance:
241
+
Наприклад:
242
242
243
243
```js run
244
244
let x = 1;
245
245
246
-
(x > 0) && alert( 'Greater than zero!' );
246
+
(x > 0) && alert( 'Більше за нуль!' );
247
247
```
248
248
249
-
The action in the right part of `&&` would execute only if the evaluation reaches it. That is, only if `(x > 0)` is true.
249
+
Дія у правій частині `&&`буде виконуватися, тільки якщо обчислення дійде до неї. Тобто, тільки якщо`(x > 0)`є ічтинним.
250
250
251
-
So we basically have an analogue for:
251
+
Тому, власне, ми маємо аналог для:
252
252
253
253
```js run
254
254
let x = 1;
255
255
256
256
if (x > 0) {
257
-
alert( 'Greater than zero!' );
257
+
alert( 'Більше за нуль!' );
258
258
}
259
259
```
260
260
261
-
The variant with `&&` appears shorter. But `if` is more obvious and tends to be a little bit more readable.
261
+
Варіант з `&&`є коротшим. Але`if`є більш очевидним і зазвичай є більш чтабельним.
262
262
263
-
So we recommend using every construct for its purpose: use `if` if we want if and use `&&` if we want AND.
263
+
Тому ми рекомендуємо використовувати кожну конструкцію за своїм призначенням: використовуємо`if`нам потрібен ifі використовуємо`&&`, якщо нам потрібенAND.
264
264
265
265
## ! (NOT)
266
266
267
-
The boolean NOT operator is represented with an exclamation sign `!`.
267
+
Булевий операторNOTпредставлений знаком оклику`!`.
268
268
269
-
The syntax is pretty simple:
269
+
Синтаксис дуже простий:
270
270
271
271
```js
272
272
result = !value;
273
273
```
274
274
275
-
The operator accepts a single argument and does the following:
275
+
Оператор приймає один аргумент і виконує наступне:
276
276
277
-
1. Converts the operand to boolean type: `true/false`.
278
-
2. Returns the inverse value.
277
+
1.Перетворює операнд на булевий тип:`true/false`.
278
+
2.Повертає зворотне значення.
279
279
280
-
For instance:
280
+
Наприклад:
281
281
282
282
```js run
283
283
alert( !true ); // false
284
284
alert( !0 ); // true
285
285
```
286
286
287
-
A double NOT `!!` is sometimes used for converting a value to boolean type:
287
+
Подвійний NOT`!!`іноді використовується для перетворення значення у булевий тип:
288
288
289
289
```js run
290
-
alert( !!"non-empty string" ); // true
290
+
alert( !!"не пустий рядок" ); // true
291
291
alert( !!null ); // false
292
292
```
293
293
294
-
That is, the first NOT converts the value to boolean and returns the inverse, and the second NOT inverses it again. In the end, we have a plain value-to-boolean conversion.
294
+
Тобто, перший NOTперетворює значення на булево і повертає зворотне, а другий NOTінвертує його знову. Зрештою, ми маємо просте перетворення значення на булевий тип.
295
295
296
-
There's a little more verbose way to do the same thing -- a built-in `Boolean` function:
296
+
Є трохи більш детальний спосіб зробити те ж саме --вбудована функція`Boolean`:
297
297
298
298
```js run
299
-
alert( Boolean("non-empty string") ); // true
299
+
alert( Boolean("не пустий рядок") ); // true
300
300
alert( Boolean(null) ); // false
301
301
```
302
302
303
-
The precedence of NOT `!` is the highest of all logical operators, so it always executes first, before `&&` or `||`.
303
+
Пріоритет NOT`!`є навищим з усіх логічних операторів, тому він завжди виконується першим, перед`&&`або`||`.
0 commit comments