本文共 1087 字,大约阅读时间需要 3 分钟。
JavaScript provides us 3 logical operators: and, or and not.
JavaScript的为我们提供了3个逻辑运算符: 和 , 或 不 。
Returns true if both operands are true:
如果两个操作数都为true,则返回true:
&&
For example:
例如:
a === true && b > 3
The cool thing about this operator is that the second expression is never executed if the first evaluates to false. Which has some practical applications, for example, to check if an object is defined before using it:
关于此运算符的很酷的事情是,如果第一个表达式的计算结果为false,则永远不会执行第二个表达式。 它具有一些实际应用,例如,在使用对象之前检查是否已定义对象:
const car = { color: 'green' }const color = car && car.color
Returns true if at least one of the operands is true:
如果至少一个操作数为true,则返回true:
||
For example:
例如:
a === true || b > 3
This operator is very useful to fallback to a default value. For example:
该运算符对于回退到默认值非常有用。 例如:
const car = {}const color = car.color || 'green'
makes color
default to green
if car.color
is not defined.
如果car.color
color
默认为green
。
Invert the value of a boolean:
反转布尔值:
let value = true!value //false
翻译自:
转载地址:http://rtqgb.baihongyu.com/