(|)和(||)之间有什么区别? [英] What's the difference between ( | ) and ( || )?
问题描述
Javascript中 |
和 ||
之间的区别是什么?
What's the difference between |
and ||
in Javascript?
此外,&
和&&
之间有什么区别?
Furthermore, what's the difference between &
and &&
?
推荐答案
|
是按位或 ||
是合乎逻辑的或。
|
is a bitwise or, ||
is a logical or.
按位或取两个数字并逐位比较它们,产生一个新的整数,它将两个输入的1位组合在一起。所以 0101 | 1010
将产生 1111
。
A bitwise or takes the two numbers and compares them on a bit-by-bit basis, producing a new integer which combines the 1 bits from both inputs. So 0101 | 1010
would produce 1111
.
逻辑或 ||
检查值的真实性(取决于类型,对于整数) 0为假,非零为真。它从左到右评估语句,并返回第一个值为truthy的值。所以 0101 || 1010
将返回 0101
这是真实的,因此整个声明都是真的。
A logical or ||
checks for the "truthiness" of a value (depends on the type, for integers 0 is false and non-zero is true). It evaluates the statement left to right, and returns the first value which is truthy. So 0101 || 1010
would return 0101
which is truthy, therefore the whole statement is said to be true.
相同类型的逻辑适用于&
vs &&
。 0101& 1010
= 0000
。但是 0101&& 1010
评估为 1010
(&&
返回最后一个真值,只要两个操作数都是真实的。)
The same type of logic applies for &
vs &&
. 0101 & 1010
= 0000
. However 0101 && 1010
evaluates to 1010
(&&
returns the last truthy value so long as both operands are truthy).
这篇关于(|)和(||)之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!