Skip to content

Commit 483de94

Browse files
committed
saving progress
1 parent bbfdd3d commit 483de94

1 file changed

Lines changed: 49 additions & 49 deletions

File tree

  • 1-js/02-first-steps/11-logical-operators

1-js/02-first-steps/11-logical-operators/article.md

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,19 @@ alert( undefined || null || 0 ); // 0 (усі не правдиві, повер
145145

146146
Присвоєння — це простий випадок. Можливі побічні ефекти, які не з'являтимуться, якщо обчислення не досяген їх.
147147
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's false, the second one is evaluated.
148+
Як ми бачимо, таке використання є "більш коротким способом виконання `if`". Перший операнд перетворюється на булевий. Якщо він false, то обчислюється другий.
149149
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`, щоб код буле легше зрозуміти, але іноді це може бути зручно.
151151
152152
## && (AND)
153153
154-
The AND operator is represented with two ampersands `&&`:
154+
Оператор AND представлений двома амперсандами `&&`:
155155
156156
```js
157157
result = a && b;
158158
```
159159
160-
In classical programming, AND returns `true` if both operands are truthy and `false` otherwise:
160+
У класичному програмуванні AND повертає `true`, якщо обидва оператора є правдивими і `false` у іншому випадку:
161161
162162
```js run
163163
alert( true && true ); // true
@@ -166,138 +166,138 @@ alert( true && false ); // false
166166
alert( false && false ); // false
167167
```
168168
169-
An example with `if`:
169+
Приклад з `if`:
170170
171171
```js run
172172
let hour = 12;
173173
let minute = 30;
174174
175175
if (hour == 12 && minute == 30) {
176-
alert( 'The time is 12:30' );
176+
alert( 'Час: 12:30' );
177177
}
178178
```
179179
180-
Just as with OR, any value is allowed as an operand of AND:
180+
Так само, як з OR, будь-яке значення дозволено як операнд AND:
181181
182182
```js run
183-
if (1 && 0) { // evaluated as true && false
184-
alert( "won't work, because the result is falsy" );
183+
if (1 && 0) { // оцінюється як true && false
184+
alert( "не буде працювати, тому що результат не правдивий" );
185185
}
186186
```
187187
188188
189-
## AND finds the first falsy value
189+
## AND шукає перше не правдиве значення
190190
191-
Given multiple AND'ed values:
191+
Дано декілька значень, об'єднаних AND:
192192

193193
```js
194194
result = value1 && value2 && value3;
195195
```
196196

197-
The AND `&&` operator does the following:
197+
Оператор AND `&&` робить наступне:
198198

199-
- Evaluates operands from left to right.
200-
- For each operand, converts it to a boolean. If the result is `false`, stops and returns the original value of that operand.
201-
- If all operands have been evaluated (i.e. all were truthy), returns the last operand.
199+
- Обчислює операнди зліва направо.
200+
- Перетворює кожен операнд на булевий. Якщо результат `false`, зупиняється і повертає оригінальне значення того операнда.
201+
- Якщо всі операнди були обчисленні (тобто усі були правдиві), повертає останній операнд.
202202

203-
In other words, AND returns the first falsy value or the last value if none were found.
203+
Іншими словами, AND повертає перше не правдиве значення, або останнє значення, якщо жодного не правдивого не було знайдено.
204204

205-
The rules above are similar to OR. The difference is that AND returns the first *falsy* value while OR returns the first *truthy* one.
205+
Правила, наведені вище, подібні до OR. Різниця полягає в тому, що AND повертає перше *не правдиве* значення, тоді як OR повертає перше *правдиве*.
206206

207-
Examples:
207+
Приклади:
208208

209209
```js run
210-
// if the first operand is truthy,
211-
// AND returns the second operand:
210+
// якщо перший операнд правдивий,
211+
// AND повертає другий операнд:
212212
alert( 1 && 0 ); // 0
213213
alert( 1 && 5 ); // 5
214214
215-
// if the first operand is falsy,
216-
// AND returns it. The second operand is ignored
215+
// якщо перший операнд не правдивий,
216+
// AND повертає саме його. Другий операнд ігнорується
217217
alert( null && 5 ); // null
218-
alert( 0 && "no matter what" ); // 0
218+
alert( 0 && "не важливо" ); // 0
219219
```
220220

221-
We can also pass several values in a row. See how the first falsy one is returned:
221+
Ми також можемо передавати декілька значень поспіль. Подивіться, як повертається перший не правдивий:
222222

223223
```js run
224224
alert( 1 && 2 && null && 3 ); // null
225225
```
226226

227-
When all values are truthy, the last value is returned:
227+
Коли всі значення є правдивими, повертається останнє значення:
228228

229229
```js run
230-
alert( 1 && 2 && 3 ); // 3, the last one
230+
alert( 1 && 2 && 3 ); // 3, останнє
231231
```
232232

233-
````smart header="Precedence of AND `&&` is higher than OR `||`"
234-
The precedence of AND `&&` operator is higher than OR `||`.
233+
````smart header="Приорітет AND `&&` є більш високим за OR `||`"
234+
Приорітет оператора AND `&&` є більш високим за OR `||`.
235235

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)`.
237237
````
238238

239-
Just like OR, the AND `&&` operator can sometimes replace `if`.
239+
Так само, як OR, оператор AND `&&` може іноді заміняти `if`.
240240

241-
For instance:
241+
Наприклад:
242242

243243
```js run
244244
let x = 1;
245245
246-
(x > 0) && alert( 'Greater than zero!' );
246+
(x > 0) && alert( 'Більше за нуль!' );
247247
```
248248

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)` є ічтинним.
250250

251-
So we basically have an analogue for:
251+
Тому, власне, ми маємо аналог для:
252252

253253
```js run
254254
let x = 1;
255255
256256
if (x > 0) {
257-
alert( 'Greater than zero!' );
257+
alert( 'Більше за нуль!' );
258258
}
259259
```
260260

261-
The variant with `&&` appears shorter. But `if` is more obvious and tends to be a little bit more readable.
261+
Варіант з `&&` є коротшим. Але `if` є більш очевидним і зазвичай є більш чтабельним.
262262

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

265265
## ! (NOT)
266266

267-
The boolean NOT operator is represented with an exclamation sign `!`.
267+
Булевий оператор NOT представлений знаком оклику `!`.
268268

269-
The syntax is pretty simple:
269+
Синтаксис дуже простий:
270270

271271
```js
272272
result = !value;
273273
```
274274

275-
The operator accepts a single argument and does the following:
275+
Оператор приймає один аргумент і виконує наступне:
276276

277-
1. Converts the operand to boolean type: `true/false`.
278-
2. Returns the inverse value.
277+
1. Перетворює операнд на булевий тип: `true/false`.
278+
2. Повертає зворотне значення.
279279

280-
For instance:
280+
Наприклад:
281281

282282
```js run
283283
alert( !true ); // false
284284
alert( !0 ); // true
285285
```
286286

287-
A double NOT `!!` is sometimes used for converting a value to boolean type:
287+
Подвійний NOT `!!` іноді використовується для перетворення значення у булевий тип:
288288

289289
```js run
290-
alert( !!"non-empty string" ); // true
290+
alert( !!"не пустий рядок" ); // true
291291
alert( !!null ); // false
292292
```
293293

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 інвертує його знову. Зрештою, ми маємо просте перетворення значення на булевий тип.
295295

296-
There's a little more verbose way to do the same thing -- a built-in `Boolean` function:
296+
Є трохи більш детальний спосіб зробити те ж саме -- вбудована функція `Boolean`:
297297

298298
```js run
299-
alert( Boolean("non-empty string") ); // true
299+
alert( Boolean("не пустий рядок") ); // true
300300
alert( Boolean(null) ); // false
301301
```
302302

303-
The precedence of NOT `!` is the highest of all logical operators, so it always executes first, before `&&` or `||`.
303+
Пріоритет NOT `!` є навищим з усіх логічних операторів, тому він завжди виконується першим, перед `&&` або `||`.

0 commit comments

Comments
 (0)