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/02-first-steps/11-logical-operators/article.md
+37-37
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
There are three logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT).
4
4
5
-
Although they are called "logical", they can be applied to values of any type, not only boolean. The result can also be of any type.
5
+
Although they are called "logical", they can be applied to values of any type, not only boolean. Their result can also be of any type.
6
6
7
7
Let's see the details.
8
8
@@ -14,9 +14,9 @@ The "OR" operator is represented with two vertical line symbols:
14
14
result = a || b;
15
15
```
16
16
17
-
In classical programming, logical OR is meant to manipulate boolean values only. If any of its arguments are `true`, then it returns `true`, otherwise it returns `false`.
17
+
In classical programming, the logical OR is meant to manipulate boolean values only. If any of its arguments are `true`, it returns `true`, otherwise it returns `false`.
18
18
19
-
In JavaScript the operator is a little bit more tricky and powerful. But first let's see what happens with boolean values.
19
+
In JavaScript, the operator is a little bit trickier and more powerful. But first, let's see what happens with boolean values.
- For each operand, convert it to boolean. If the result is `true`, then stop and return the original value of that operand.
83
-
- If all other operands have been assessed (i.e. all were `false`), return the last operand.
81
+
-Evaluates operands from left to right.
82
+
- For each operand, converts it to boolean. If the result is `true`, stops and returns the original value of that operand.
83
+
- If all operands have been evaluated (i.e. all were `false`), returns the last operand.
84
84
85
85
A value is returned in its original form, without the conversion.
86
86
@@ -97,13 +97,13 @@ alert( null || 0 || 1 ); // 1 (the first truthy value)
97
97
alert( undefined||null||0 ); // 0 (all falsy, returns the last value)
98
98
```
99
99
100
-
That leads to some interesting usages compared to a "pure, classical, boolean-only OR".
100
+
This leads to some interesting usage compared to a "pure, classical, boolean-only OR".
101
101
102
-
1.**Getting the first truthy value from the list of variables or expressions.**
102
+
1.**Getting the first truthy value from a list of variables or expressions.**
103
103
104
-
Imagine we have several variables, which can either contain the data or be `null/undefined`. And we need to choose the first one with data.
104
+
Imagine we have several variables which can either contain data or be `null/undefined`. How can we find the first one with data?
105
105
106
-
We can use OR `||` for that:
106
+
We can use OR `||`:
107
107
108
108
```js run
109
109
let currentUser =null;
@@ -116,14 +116,14 @@ That leads to some interesting usages compared to a "pure, classical, boolean-on
116
116
alert( name ); // selects "John" – the first truthy value
117
117
```
118
118
119
-
If both `currentUser` and `defaultUser` were falsy then`"unnamed"` would be the result.
119
+
If both `currentUser` and `defaultUser` were falsy,`"unnamed"` would be the result.
120
120
2.**Short-circuit evaluation.**
121
121
122
-
Operands can be not only values, but arbitrary expressions. OR evaluates and tests them from left to right. The evaluation stops when a truthy value is reached, and the value is returned. Theprocess is called "a short-circuit evaluation", because it goes as short as possible from left to right.
122
+
Operands can be not only values, but arbitrary expressions. OR evaluates 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.
123
123
124
-
This is clearly seen when the expression given as the second argument has a side effect. Like a variable assignment.
124
+
This is clearly seen when the expression given as the second argument has a side effect like a variable assignment.
125
125
126
-
If we run the example below, `x`would not get assigned:
126
+
In the example below, `x`does not get assigned:
127
127
128
128
```js run no-beautify
129
129
let x;
@@ -133,7 +133,7 @@ That leads to some interesting usages compared to a "pure, classical, boolean-on
133
133
alert(x); // undefined, because (x = 1) not evaluated
134
134
```
135
135
136
-
...And if the first argument is `false`, then `OR`goes on and evaluates the second one thus running the assignment:
136
+
If, instead, the first argument is `false`, `OR` evaluates the second one, thus running the assignment:
137
137
138
138
```js run no-beautify
139
139
let x;
@@ -143,11 +143,11 @@ That leads to some interesting usages compared to a "pure, classical, boolean-on
143
143
alert(x); // 1
144
144
```
145
145
146
-
An assignment is a simple case, other side effects can be involved.
146
+
An assignment is a simple case. Other side effects can also be involved.
147
147
148
-
As we can see, such a use case is a "shorter way to do `if`". The first operand is converted to boolean and ifit's false then the second one is evaluated.
148
+
As we can see, such a use case is a "shorter way of doing `if`". The first operand is converted to boolean. Ifit's false, the second one is evaluated.
149
149
150
-
Most of time it's better to use a "regular"`if` to keep the code easy to understand, but sometimes that can be handy.
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.
151
151
152
152
## && (AND)
153
153
@@ -157,7 +157,7 @@ The AND operator is represented with two ampersands `&&`:
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
+
In classical programming,AND returns `true`if both operands are truthy and `false` otherwise:
161
161
162
162
```js run
163
163
alert( true && true ); // true
@@ -173,11 +173,11 @@ let hour = 12;
173
173
let minute = 30;
174
174
175
175
if (hour == 12 && minute == 30) {
176
-
alert( 'Time is 12:30' );
176
+
alert( 'The time is 12:30' );
177
177
}
178
178
```
179
179
180
-
Just as forOR, any value is allowed as an operand ofAND:
180
+
Just as withOR, any value is allowed as an operand ofAND:
181
181
182
182
```js run
183
183
if (1 && 0) { // evaluated as true && false
@@ -186,7 +186,7 @@ if (1 && 0) { // evaluated as true && false
That is, the first NOT converts the value to boolean and returns the inverse, and the second NOT inverses it again. At the end we have a plain value-to-boolean conversion.
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.
295
295
296
296
There's a little more verbose way to do the same thing -- a built-in`Boolean` function:
0 commit comments