Javascript条件和逻辑运算符

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) returns false. The only falsy values in JS are false, 0, '', null, undefined and NaN.

    Falsy是一个Boolean(value)返回false Boolean(value) 。 JS中唯一的错误值是false0''nullundefinedNaN

  • Truthy is a value for which Boolean(value) returns true. 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。 例如,如果expr1true ,则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 truewithout 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值