Skip to content

Commit 282a0d2

Browse files
bemselfMartinsYongleviding
authored
pr764-3: 1-js/02-first-steps/11-logical-operators/ (javascript-tutorial#776)
* init 1-02-11 * Update article.md * Update 1-js/02-first-steps/11-logical-operators/article.md Co-authored-by: Martin <[email protected]> * Update 1-js/02-first-steps/11-logical-operators/article.md Co-authored-by: Martin <[email protected]> * Update 1-js/02-first-steps/11-logical-operators/article.md Co-authored-by: LeviDing <[email protected]> * Update 1-js/02-first-steps/11-logical-operators/article.md Co-authored-by: LeviDing <[email protected]> * Update 1-js/02-first-steps/11-logical-operators/article.md Co-authored-by: LeviDing <[email protected]> * Update 1-js/02-first-steps/11-logical-operators/article.md Co-authored-by: LeviDing <[email protected]> * Update article.md * Update article.md * Update 1-js/02-first-steps/11-logical-operators/article.md * Update 1-js/02-first-steps/11-logical-operators/article.md * Update 1-js/02-first-steps/11-logical-operators/article.md * Update 1-js/02-first-steps/11-logical-operators/article.md * Update 1-js/02-first-steps/11-logical-operators/article.md * Update 1-js/02-first-steps/11-logical-operators/article.md Co-authored-by: Martin <[email protected]> Co-authored-by: LeviDing <[email protected]>
1 parent 6d2d89a commit 282a0d2

File tree

1 file changed

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

1 file changed

+15
-15
lines changed

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
JavaScript 里有三个逻辑运算符:`||`(或),`&&`(与),`!`(非)。
44

5-
虽然他们被称为“逻辑”运算符,但这些运算符却可以被应用于任意类型的值,而不仅仅是布尔值。他们的结果也同样可以是任意类型
5+
虽然它们被称为“逻辑”运算符,但这些运算符却可以被应用于任意类型的值,而不仅仅是布尔值。它们的结果也同样可以是任意类型
66

77
让我们来详细看一下。
88

@@ -84,7 +84,7 @@ result = value1 || value2 || value3;
8484

8585
返回的值是操作数的初始形式,不会做布尔转换。
8686

87-
也就是,一个或 `"||"` 运算的链,将返回第一个真值,如果不存在真值,就返回该链的最后一个值。
87+
也就是,一个或运算 `"||"` 的链,将返回第一个真值,如果不存在真值,就返回该链的最后一个值。
8888

8989
例如:
9090

@@ -101,9 +101,9 @@ alert( undefined || null || 0 ); // 0(所有的转化结果都是 false,返
101101

102102
1. **获取变量列表或者表达式的第一个真值。**
103103

104-
For instance, we have `firstName`, `lastName` and `nickName` variables, all optional.
104+
例如,我们有 `firstName``lastName` `nickName` 变量,都是可选的。
105105

106-
Let's use OR `||` to choose the one that has the data and show it (or `anonymous` if nothing set):
106+
我们用或运算 `||` 来选择有值的那一个,并显示出来(如果没有设置,则用 `匿名(anonymous`):
107107

108108
```js run
109109
let firstName = "";
@@ -115,26 +115,26 @@ alert( undefined || null || 0 ); // 0(所有的转化结果都是 false,返
115115
*/!*
116116
```
117117

118-
If all variables were falsy, `Anonymous` would show up.
118+
如果所有变量都为假(falsy),结果就是 `Anonymous`
119119

120-
2. **Short-circuit evaluation.**
120+
2. **短路求值(Short-circuit evaluation)。**
121121

122-
Another feature of OR `||` operator is the so-called "short-circuit" evaluation.
122+
或运算符 `||` 的另一个特点是所谓的“短路求值”。
123123

124-
It means that `||` processes its arguments until the first truthy value is reached, and then the value is returned immediately, without even touching the other argument.
124+
它的意思是,`||` 对其参数进行处理,直到达到第一个真值,然后立即返回该值,而无需处理其他参数。
125125

126-
That importance of this feature becomes obvious if an operand isn't just a value, but an expression with a side effect, such as a variable assignment or a function call.
126+
如果操作数不仅仅是一个值,而是一个有副作用的表达式,例如变量赋值或函数调用,那么这一特性的重要性就变得显而易见了。
127127

128-
In the example below, only the second message is printed:
128+
在下面这个例子中,只会打印第二条信息:
129129

130130
```js run no-beautify
131131
*!*true*/!* || alert("not printed");
132132
*!*false*/!* || alert("printed");
133133
```
134134

135-
In the first line, the OR `||` operator stops the evaluation immediately upon seeing `true`, so the `alert` isn't run.
135+
在第一行中,或运算符 `||` 在遇到 `true` 时立即停止运算,所以 `alert` 没有运行。
136136

137-
Sometimes, people use this feature to execute commands only if the condition on the left part is falsy.
137+
有时,人们利用这个特性,只在左侧的条件为假时才执行命令。
138138

139139
## &&(与)
140140

@@ -223,8 +223,8 @@ alert( 1 && 2 && 3 ); // 3,最后一个值
223223
所以代码 `a && b || c && d` 完全跟 `&&` 表达式加了括号一样:`(a && b) || (c && d)`
224224
````
225225

226-
````warn header="Don't replace `if` with || or &&"
227-
Sometimes, people use the AND `&&` operator as a "shorter to write `if`".
226+
````warn header="不要用 || && 来取代 `if`"
227+
有时候,有人会用与运算符 `&&` 来“简化 `if`”。
228228

229229
例如:
230230

@@ -244,7 +244,7 @@ let x = 1;
244244
if (x > 0) alert( 'Greater than zero!' );
245245
```
246246

247-
Although, the variant with `&&` appears shorter, `if` is more obvious and tends to be a little bit more readable. So we recommend using every construct for its purpose: use `if` if we want if and use `&&` if we want AND.
247+
虽然使用 `&&` 写出的变体看起来更短,但 `if` 更明显,并且往往更具可读性。因此,我们建议根据每个语法结构的用途来使用:如果我们想要 `if`,就使用 `if`;如果我们想要逻辑与,就使用 `&&`
248248
````
249249

250250

0 commit comments

Comments
 (0)