Some things to bear in mind before I get started:
开始之前,请记住以下几点:
- JavaScript is a loosely typed language which means logical operations can be performed on any type. JavaScript是一种松散类型的语言,这意味着可以对任何类型执行逻辑操作。
Falsy is a value for which
Boolean(value)
returnsfalse
. The only falsy values in JS arefalse
,0
,''
,null
,undefined
andNaN
.Falsy是一个
Boolean(value)
返回false
Boolean(value)
。 JS中唯一的错误值是false
,0
,''
,null
,undefined
和NaN
。Truthy is a value for which
Boolean(value)
returnstrue
. In JS, truthy values are essentially non-falsy values.Truthy是一个
Boolean(value)
返回true
Boolean(value)
。 在JS中, 真实值本质上是非虚假值。
三元运算符 (Ternary operator)
JavaScript also contains a conditional operator commonly referred to as the “ternary operator”.
JavaScript还包含一个条件运算符,通常称为“三元运算符”。
condition ? conditionIsTrue : conditionisFalse
condition ? conditionIsTrue : conditionisFalse
If the
condition
evaluates to a truthy value,conditionIsTrue
will be executed如果
condition
评估为真值,则将执行conditionIsTrue
If the
condition
evaluates to a falsy value,conditionIsFalse
will be executed如果
condition
评估为假值,则将执行conditionIsFalse
I mostly use it to quickly write simple conditional statements, like these:
我主要使用它来快速编写简单的条件语句,如下所示:
weather = {sunny: true, cloudy: true}weather.sunny? 'Remember your sunglasses!' : 'No sun today :('weather.sunny ? (weather.cloudy ? 'It is sunny and cloudy' : 'It is sunny and not a cloud in sight') : 'No sun today :('
Personally, I would recommend using if..else
statements as opposed to using the ternary/conditional operator for nested ternaries because they are infinitely more readable.
就个人而言,我建议使用if..else
语句,而不是对嵌套三元使用三元/条件运算符,因为它们无限地易读。
weather.sunny ? (weather.cloudy ? 'It is sunny and cloudy' : 'It is sunny and not a cloud in sight') : 'No sun today :('vsif (weather.sunny){
if(weather.cloudy){
} else {
'It is sunny and not a cloud in sight'
}
} else {
'No sun today :('
}
And this can be further simplified and made more readable using logical AND operator (&&).
并且可以使用逻辑AND运算符(&&)进一步简化并使其更具可读性。
逻辑AND && (Logical AND &&)
The logical AND (&&
) operator returns true if and only if all of its operands are true otherwise, it returns false. For example, expr1 && expr2
returns expr2
if expr1
is true
; otherwise, it will return expr1
.
逻辑AND( &&
)运算符仅在其所有操作数均为true时返回true,否则返回false。 例如,如果expr1
为true
,则expr1 && expr2
返回expr2
。 否则,它将返回expr1
。
expr1 && expr2weather = {sunny: true, cloudy: true, rainy: false, windy: false}weather.sunny && weather.cloudy; // true
weather.sunny && weather.rainy; // false
weather.rainy && weather.sunny; // false
weather.rainy && weather.windy; // false
In the weather example, I was using above, you could write it like this:
在我上面使用的天气示例中,您可以这样编写:
weather = {sunny: true, cloudy: true}if (weather.sunny && weather.cloudy) {
} else if (weather.sunny) {
'It is sunny and not a cloud in sight'
} else {
'No sun today :('
}//
You can chain multiple && operands and it will continue to the following expression if the previous expression is true. Using &&
will return the first false
or ‘falsy’ value and if every expression evaluates to true
, the last evaluated expression will be returned.
您可以链接多个&&操作数,并且如果前一个表达式为true,它将继续到下一个表达式。 使用&&
将返回第一个false
或'falsy'值,如果每个表达式的计算结果为true
,则将返回最后一个计算的表达式。
逻辑或|| (Logical OR ||)
The logical OR operator (||) returns true
if either or both expression is true
and returns false
otherwise.
逻辑OR运算符(||)返回true
,如果任一个或两个表达式为 true
,并返回false
,否则。
expr1 || expr2
This operator uses short-circuiting because it is evaluated left to right. This means that if the first expression is truthy, then it will immediately return true
without evaluating the second expression which can be a disadvantage at times.
该运算符使用短路,因为它是从左到右评估的。 这意味着,如果第一个表达式是真实的,那么它将立即返回true
而不评估第二个表达式,这有时可能是不利的。
weather = {sunny: true, cloudy: true, rainy: false, windy: false}weather.sunny || weather.cloudy; // true
weather.sunny || weather.rainy; // true
weather.rainy || weather.sunny; // true
weather.rainy || weather.windy; // falseweather.sunny || true; // true
true || false; // true
false || true; // true
false || false; // false
翻译自: https://medium.com/analytics-vidhya/javascript-conditional-and-logical-operators-98a5a40ee693