JavaScript逻辑运算符

Logical operators compare Boolean values and return a Boolean response. There are two types of logical operators - Logical AND, and Logical OR. These operators are often written as && for AND, and || for OR.

逻辑运算符比较布尔值并返回布尔响应。 逻辑运算符有两种类型-逻辑与和逻辑或。 这些运算符通常用AND表示为&&和||。 或。

逻辑AND(&&) (Logical AND ( && ))

The AND operator compares two expressions. If the first evaluates as “truthy”, the statement will return the value of the second expression. If the first evaluates as “falsy”, the statement will return the value of the first expression.

AND运算符比较两个表达式。 如果第一个表达式的值为“ truthy” ,则该语句将返回第二个表达式的值。 如果第一个表达式的值为“ falsy” ,则该语句将返回第一个表达式的值。

When only involving boolean values (either true or false), it returns true if only if both expressions are true. If one or both expressions are false, the entire statement will return false.

当仅涉及布尔值( truefalse )时,仅当两个表达式均为true时,它才返回true。 如果一个或两个表达式为假,则整个语句将返回假。

true && true //returns  the second value, true
true && false //returns the second value, false
false && false //returns the first value, false
123 && 'abc' // returns the second value, 'abc'
'abc' && null //returns the second value, null
undefined && 'abc' //returns the first value, undefined
0 && false //returns the first value, 0

逻辑或(||) (Logical OR ( || ))

The OR operator compares two expressions. If the first evaluates as “falsy”, the statement will return the value of the second second expression. If the first evaluates as “truthy”, the statement will return the value of the first expression.

OR运算符比较两个表达式。 如果第一个计算结果为“ falsy”,则该语句将返回第二个第二个表达式的值。 如果第一个表达式的计算结果为“真实”,则该语句将返回第一个表达式的值。

When only involving boolean values (either true or false), it returns true if either expression is true. Both expressions can be true, but only one is needed to get true as a result.

当仅涉及布尔值( truefalse )时,如果任一表达式为true,则返回true。 这两个表达式都可以为真,但只需要一个表达式即可为真。

true || true //returns the first value, true
true || false //returns the first value, true
false || false //returns the second value, false
123 || 'abc' // returns the first value, 123
'abc' || null //returns the first value, 'abc'
undefined || 'abc' //returns the second value, 'abc'
0 || false //returns the second value, false

短路评估 (Short-circuit evaluation)

&& and || behave as a short-circuit operators.

&&和|| 充当短路操作员。

In case of the logical AND, if the first operand returns false, the second operand is never evaluated and first operand is returned.

在逻辑与的情况下,如果第一个操作数返回false,则从不评估第二个操作数,并返回第一个操作数。

In case of the logical OR, if the first value returns true, the second value is never evaluated and the first operand is returned.

在逻辑或的情况下,如果第一个值返回true,则从不评估第二个值,并返回第一个操作数。

逻辑非(!) (Logical NOT (!))

The NOT operator does not do any comparison like the AND and OR operators.Moreover it is operated only on 1 operand.

NOT运算符不会像AND和OR运算符那样进行任何比较,而且只能在1个操作数上进行运算。

An ’!’ (exclamation) mark is used for representing the NOT operator.

一个'!' (感叹号)标记用于表示NOT运算符。

使用NOT运算符 (Use of NOT operators)

  1. conversion of the expression into boolean.

    将表达式转换为布尔值。
  2. returns the inverse of the boolean value obtained in last step.

    返回上一步获得的布尔值的倒数。
var spam = 'rinki'; //spam may be equal to any non empty string
var booSpam = !spam;
/*returns false
  since when a non-empty string when converted to boolean returns true
  inverse of which is evaluated to false.
*/

var spam2 = ''; //spam2 here is equal to empty string
var booSpam2 = !spam2;
/*returns true
  since when a empty string when converted to boolean returns false
  inverse of which is evaluated to true.
*/

提示: (Tips:)

Both logical operators will return the value of the last evaluated expression. For example:

两个逻辑运算符都将返回最后一个求值表达式的值。 例如:

"cat" && "dog" //returns "dog"
"cat" && false //returns false
0 && "cat"  //returns 0 (which is a falsy value)

"cat" || "dog" //returns "cat"
"cat" || false //returns "cat"
0 || "cat" //returns "cat"

Note that where && returns the first value, || returns the second value and vice versa.

注意, &&返回第一个值, || 返回第二个值,反之亦然。

翻译自: https://www.freecodecamp.org/news/javascript-logical-operators/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值