Logical Operators

JavaScript Logical Operators (JavaScript逻辑运算符)

Logical operators are typically used to determine the logic between variables or values. They return a Boolean value of true or false depending on the evaluation. There are three logical operators in JavaScript: || (OR), && (AND), ! (NOT).

They are called “logical”, but can be applied to values of any type, their result can also be of any type. (它们被称为“逻辑” ,但可以应用于任何类型的值,它们的结果也可以是任何类型的。)

|| (OR)

|| (OR)

The OR operator is performed with two vertical lines and can manipulate boolean values only. The logical || (OR) can manipulate boolean values only. In case any of its arguments are true, it returns true, if not, it returns false. With this operator, at least one of the statements has to evaluate to true, otherwise it returns false.

boolValue = a || b;

There are four possible logical combinations:

console.log( true || true );   // true
console.log( false || true );  // true
console.log( true || false );  // true
console.log( false || false ); // false

As you see, the result is always true except for the case when both operands are false. If an operand isn’t a boolean, then it’s converted to a boolean for the rating. (如您所见,结果始终为true ,但两个操作数均为false的情况除外。如果操作数不是布尔值,则将其转换为评级的布尔值。)

For instance, the number 1 is attended as true, the number 0 as false:

if (1 || 0) { //works the same way( true || false )
 console.log('true!');
}

Sometimes || (OR) is used in an if statement to test if any of the given conditions is true.

let age = 10;
if (age > 0 || age < 18) {
 console.log('You are too young');
}

|| (OR) finds the first truthy value

|| (OR) finds the first truthy value

Described above logic is classical. Now let’s discuss the “extra” attributes of JavaScript. The extended algorithm works as follows. (上述逻辑是经典的。现在,让我们讨论JavaScript的“额外”属性。扩展算法的工作原理如下。)

Given multiple OR’ed values:

result = value1 || value2 || value3;

The || (OR) operator executes the following things:

  • Evaluates operands from left to right. (-从左到右计算操作数。)

  • For each operand, converts it to boolean. In case the result is true, it stops and returns the original value of that operand. (-对于每个操作数,将其转换为布尔值。如果结果为真,则停止并返回该操作数的原始值。)

  • If all operands are false, it returns the last operand. (-如果所有操作数均为false ,则返回最后一个操作数。)

A value is returned in its initial form, without any changes. (以初始形式返回值,不做任何更改。)

console.log( 0 || 1); // 1 (1 is truthy)
console.log( true || 'anything' ); // (true is truthy)
console.log( 1 || null ); // 1 (1 is truthy value)
console.log( null || 0 || 1 ); // 1 (1 is the first truthy value)
console.log( undefined || null || 0 ); // 0 ( returns the last value, because all falsy )

&& (AND)

&& (AND) (和)

The AND operator is performed with two ampersands &&and returns true if both operands are truthy and false otherwise:

console.log( true && true );   // true
console.log( false && true );  // false
console.log( true && false );  // false
console.log( false && false ); // false

Example of the AND operator with if:

let hour = 11;
if (hour > 10 && hour < 18) {
 console.log('We are open!');
}

&& (AND) finds the first falsy value

&& (AND) finds the first falsy value (& & (AND)查找第一个伪值)

Given multiple AND’ed values:

result = value1 && value2 && value3;

The AND && operator makes the following:

  • Asses operands from left to right. (-从左到右评估操作数。)

  • Converts it to a boolean for each operand. In case the result is false, it stops and returns the original value of that operand. (-将其转换为每个操作数的布尔值。如果结果为false ,则停止并返回该操作数的原始值。)

  • If all operands were truthy, it returns the last operand. (-如果所有操作数均为真,则返回最后一个操作数。)

Shortly, AND returns the first falsy value or the last value if none were found. (很快,和返回第一个伪值,如果没有找到,则返回最后一个值。)

The rules above are similar to OR. The difference between them is that AND returns the first falsy value while OR returns the first truthy one. (上述规则与OR类似。它们之间的区别在于, AND返回第一个伪值,而OR返回第一个真值。)

AND returns the second operand if the first operand is truthy:

console.log( 1 && 0 ); // 0
console.log( 1 && 6); // 6

AND returns the first operand, if it is false, the second operand is ignored:

console.log( null && 6 ); // null
console.log( 0 && "anything" ); // 0

! (NOT)

! (NOT) (不!)

The logical NOT operator is represented with an exclamation sign ! (NOT) . It reverses or negates the value of a boolean. (逻辑NOT运算符用感叹号表示! (NOT)。它颠倒或否定布尔值。)

It’s syntax is very simple:

result = !value;

The operator accepts a single argument and does presented below functions:

  • Transforms the operand to boolean type: true/false.

  • Returns the opposite value. (-返回相反的值。)

Example of the NOT operator:

		javascript Not operator

console.log( !false ); // true console.log( !1 ); // false

			Run >
		

			Reset
console.log( !false ); // true
console.log( !1 ); // false

Sometimes we use a double !! (NOT) to convert a value to boolean type:

console.log( !!"not-empty string" ); // true
console.log( !!null ); // false

The first NOT transforms the value to boolean and returns the opposite, the second NOT inverses it again. After all, we have a plain value-to-boolean conversion. (第一个NOT将值转换为布尔值并返回相反的值,第二个NOT再次将其反转。毕竟,我们有一个简单的值到布尔的转换。)

There’s a little more wordy way to do the same thing – a built-in Boolean function:

console.log( Boolean("not-empty string") ); // true
console.log( Boolean(null) ); // false

The priority of ! (NOT) is the highest of all logical operators, that’s why it always executes first, before && or ||.



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部