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
1.**Отримання першого правдивого значення зі списку змінних або виразів.**
103
103
104
-
Imagine we have a list of variables which can either contain data or be `null/undefined`. How can we find the first one with data?
104
+
Уявіть, що у нас є список змінних, які можуть містити дані убо бути `null/undefined`. Як знайти першу змінну за даними?
105
105
106
-
We can use OR `||`:
106
+
Ми можемо використати OR `||`:
107
107
108
108
```js run
109
109
let currentUser =null;
110
-
let defaultUser ="John";
110
+
let defaultUser ="Іван";
111
111
112
112
*!*
113
113
let name = currentUser || defaultUser ||"unnamed";
114
114
*/!*
115
115
116
-
alert( name ); //selects "John" – the first truthy value
116
+
alert( name ); //обере "Іван" – перше правдиве значення
117
117
```
118
118
119
-
If both`currentUser` and`defaultUser`were falsy, `"unnamed"`would be the result.
120
-
2.**Short-circuit evaluation.**
119
+
Якщо і`currentUser`, і`defaultUser`були б не правдивими, `"unnamed"`було б результатом.
120
+
2.**Обчислення короткого змичання.**
121
121
122
-
Operands can be not only values, but arbitrary expressions. ORevaluates and tests them from left to right. The evaluation stops when a truthy value is reached, and the value is returned. Thisprocess is called "a short-circuit evaluation" because it goes as short as possible from left to right.
122
+
Операндами можуть бути не тільки значення, але й довільні вирази. ORобчислює Цей процес називається "обчисленням короткого замикання", оскільки він іде якомога коротше зліва направо.
123
123
124
-
This is clearly seen when the expression given as the second argument has a side effect like a variable assignment.
124
+
Це чітко видно, коли вираз, заданий як другий аргумент, має побічний ефект, як присвоєння змінної.
125
125
126
-
In the example below,`x`does not get assigned:
126
+
У наведеному нижче прикладі`x`не присвоюється значення:
127
127
128
128
```js run no-beautify
129
129
let x;
130
130
131
131
*!*true*/!* || (x = 1);
132
132
133
-
alert(x); // undefined, because (x = 1) not evaluated
133
+
alert(x); // undefined, оскільки (x = 1) не був обчислений
134
134
```
135
135
136
-
If, instead, the first argument is`false`, `||`evaluates the second one, thus running the assignment:
136
+
Якщо замість цього перший аргумент є`false`, `||`обчислює другий, таким чином виконуючи присвоєння:
An assignment is a simple case. There may be side effects, that won't show up if the evaluation doesn't reach them.
146
+
Присвоєння — це простий випадок. Можливі побічні ефекти, які не з'являтимуться, якщо обчислення не досяген їх.
147
147
148
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.
0 commit comments