Skip to content

Commit c214c2c

Browse files
authored
Update 02-first-steps/11-logical-operators
1 parent 261f94b commit c214c2c

File tree

1 file changed

+37
-37
lines changed
  • 1-js/02-first-steps/11-logical-operators

1 file changed

+37
-37
lines changed

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

+37-37
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
There are three logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT).
44

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

77
Let's see the details.
88

@@ -14,9 +14,9 @@ The "OR" operator is represented with two vertical line symbols:
1414
result = a || b;
1515
```
1616

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`.
1818

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

2121
There are four possible logical combinations:
2222

@@ -29,17 +29,17 @@ alert( false || false ); // false
2929

3030
As we can see, the result is always `true` except for the case when both operands are `false`.
3131

32-
If an operand is not boolean, then it's converted to boolean for the evaluation.
32+
If an operand is not a boolean, it's converted to a boolean for the evaluation.
3333

34-
For instance, a number `1` is treated as `true`, a number `0` -- as `false`:
34+
For instance, the number `1` is treated as `true`, the number `0` as `false`:
3535

3636
```js run
3737
if (1 || 0) { // works just like if( true || false )
3838
alert( 'truthy!' );
3939
}
4040
```
4141

42-
Most of the time, OR `||` is used in an `if` statement to test if *any* of the given conditions is correct.
42+
Most of the time, OR `||` is used in an `if` statement to test if *any* of the given conditions is `true`.
4343

4444
For example:
4545

@@ -64,9 +64,9 @@ if (hour < 10 || hour > 18 || isWeekend) {
6464
}
6565
```
6666

67-
## OR seeks the first truthy value
67+
## OR finds the first truthy value
6868

69-
The logic described above is somewhat classical. Now let's bring in the "extra" features of JavaScript.
69+
The logic described above is somewhat classical. Now, let's bring in the "extra" features of JavaScript.
7070

7171
The extended algorithm works as follows.
7272

@@ -78,9 +78,9 @@ result = value1 || value2 || value3;
7878

7979
The OR `||` operator does the following:
8080

81-
- Evaluate operands from left to right.
82-
- 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.
8484

8585
A value is returned in its original form, without the conversion.
8686

@@ -97,13 +97,13 @@ alert( null || 0 || 1 ); // 1 (the first truthy value)
9797
alert( undefined || null || 0 ); // 0 (all falsy, returns the last value)
9898
```
9999

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".
101101

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.**
103103

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?
105105

106-
We can use OR `||` for that:
106+
We can use OR `||`:
107107

108108
```js run
109109
let currentUser = null;
@@ -116,14 +116,14 @@ That leads to some interesting usages compared to a "pure, classical, boolean-on
116116
alert( name ); // selects "John" – the first truthy value
117117
```
118118

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.
120120
2. **Short-circuit evaluation.**
121121

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. The process 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. This process is called "a short-circuit evaluation" because it goes as short as possible from left to right.
123123

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

126-
If we run the example below, `x` would not get assigned:
126+
In the example below, `x` does not get assigned:
127127

128128
```js run no-beautify
129129
let x;
@@ -133,7 +133,7 @@ That leads to some interesting usages compared to a "pure, classical, boolean-on
133133
alert(x); // undefined, because (x = 1) not evaluated
134134
```
135135

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:
137137

138138
```js run no-beautify
139139
let x;
@@ -143,11 +143,11 @@ That leads to some interesting usages compared to a "pure, classical, boolean-on
143143
alert(x); // 1
144144
```
145145

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

148-
As we can see, such a use case is a "shorter way to do `if`". The first operand is converted to boolean and if it'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. If it's false, the second one is evaluated.
149149
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.
151151

152152
## && (AND)
153153

@@ -157,7 +157,7 @@ The AND operator is represented with two ampersands `&&`:
157157
result = a && b;
158158
```
159159

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:
161161

162162
```js run
163163
alert( true && true ); // true
@@ -173,11 +173,11 @@ let hour = 12;
173173
let minute = 30;
174174
175175
if (hour == 12 && minute == 30) {
176-
alert( 'Time is 12:30' );
176+
alert( 'The time is 12:30' );
177177
}
178178
```
179179

180-
Just as for OR, any value is allowed as an operand of AND:
180+
Just as with OR, any value is allowed as an operand of AND:
181181

182182
```js run
183183
if (1 && 0) { // evaluated as true && false
@@ -186,7 +186,7 @@ if (1 && 0) { // evaluated as true && false
186186
```
187187

188188

189-
## AND seeks the first falsy value
189+
## AND finds the first falsy value
190190

191191
Given multiple AND'ed values:
192192
@@ -196,9 +196,9 @@ result = value1 && value2 && value3;
196196
197197
The AND `&&` operator does the following:
198198
199-
- Evaluate operands from left to right.
200-
- For each operand, convert it to a boolean. If the result is `false`, stop and return the original value of that operand.
201-
- If all other operands have been assessed (i.e. all were truthy), return the last operand.
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.
202202
203203
In other words, AND returns the first falsy value or the last value if none were found.
204204
@@ -233,7 +233,7 @@ alert( 1 && 2 && 3 ); // 3, the last one
233233
````smart header="Precedence of AND `&&` is higher than OR `||`"
234234
The precedence of AND `&&` operator is higher than OR `||`.
235235
236-
So the code `a && b || c && d` is essentially the same as if `&&` were in parentheses: `(a && b) || (c && d)`.
236+
So the code `a && b || c && d` is essentially the same as if the `&&` expressions were in parentheses: `(a && b) || (c && d)`.
237237
````
238238
239239
Just like OR, the AND `&&` operator can sometimes replace `if`.
@@ -246,7 +246,7 @@ let x = 1;
246246
(x > 0) && alert( 'Greater than zero!' );
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+
The action in the right part of `&&` would execute only if the evaluation reaches it. That is, only if `(x > 0)` is true.
250250
251251
So we basically have an analogue for:
252252
@@ -258,9 +258,9 @@ if (x > 0) {
258258
}
259259
```
260260
261-
The variant with `&&` appears to be shorter. But `if` is more obvious and tends to be a little bit more readable.
261+
The variant with `&&` appears shorter. But `if` is more obvious and tends to be a little bit more readable.
262262
263-
So it is recommended to use every construct for its purpose. Use `if` if we want if. And use `&&` if we want AND.
263+
So we recommend using every construct for its purpose: use `if` if we want if and use `&&` if we want AND.
264264
265265
## ! (NOT)
266266
@@ -275,7 +275,7 @@ result = !value;
275275
The operator accepts a single argument and does the following:
276276
277277
1. Converts the operand to boolean type: `true/false`.
278-
2. Returns an inverse value.
278+
2. Returns the inverse value.
279279
280280
For instance:
281281
@@ -291,7 +291,7 @@ alert( !!"non-empty string" ); // 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. 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.
295295
296296
There's a little more verbose way to do the same thing -- a built-in `Boolean` function:
297297

@@ -300,4 +300,4 @@ alert( Boolean("non-empty string") ); // 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 any `&&`, `||`.
303+
The precedence of NOT `!` is the highest of all logical operators, so it always executes first, before `&&` or `||`.

0 commit comments

Comments
 (0)