简体   繁体   English

Javascript 中是否有 & 逻辑运算符

[英]Is there a & logical operator in Javascript

I was wondering if there is a "&" logical operator in Javascript.我想知道 Javascript 中是否有“&”逻辑运算符。 I tried running 1 & 0 and 1 && 0 in Firebug(Firefox) and it returned a 0 for both.我尝试在 Firebug(Firefox) 中运行 1 & 0 和 1 && 0 并且两者都返回 0 。

Someone told me that C# accepts both & and double &&, double being more efficient as it will exit the comparison loop as soon as a false is encountered, but I was not able to find any info for Javascript on that.有人告诉我 C# 接受 & 和 double &&,double 效率更高,因为它会在遇到 false 时立即退出比较循环,但我无法找到有关 Javascript 的任何信息。

Any ideas?有任何想法吗?

No. & is a bitwise AND operator.不。 &按位 AND运算符。 && is the only logical AND operator in Javascript. &&是 Javascript 中唯一的逻辑 AND 运算符。

The && operator returns 0 for the expression 1 && 0 because its semantics are different than those of the same operator (well, symbolically the same) in other C-like languages. &&运算符为表达式1 && 0返回0 ,因为它的语义与其他类 C 语言中的相同运算符的语义不同(好吧,符号上相同)。

In Javascript, the && operator does coerce its operands to boolean values, but only for the purposes of evaluation.在 Javascript 中, &&运算符确实将其操作数强制为布尔值,但用于评估目的。 The result of an expression of the form形式表达式的结果

e1 && e2 && e3 ...

is the actual value of the first subexpression en whose coerced boolean value is false .是强制布尔值为false的第一个子表达式en实际值。 If they're all true when coerced to boolean, then the result is the actual value of the last en .如果强制为布尔值时它们都为true ,则结果是最后一个en实际值。 Similarly, the ||同样, || operator interprets an expression like this:运算符解释这样的表达式:

e1 || e2 || e3 ...

by returning the actual value of the first en whose coerced boolean value is true .通过返回强制布尔值为true的第一个en实际值。 If they're all false, then the value is the actual value of the last one.如果它们都是假的,那么该值就是最后一个的实际值。

Implicit in those descriptions is the fact that both && and ||这些描述中隐含的事实是&&|| stop evaluating the subexpressions as soon as their conditions for completion are met.一旦满足完成条件,就停止计算子表达式。

1 & 0 is 0. 1 & 0 是 0。

It's a bitwise operator, not a logical operator.它是一个按位运算符,而不是一个逻辑运算符。

&& means a logical AND of the left and right operators. && 表示左右运算符的逻辑与。 This means it will return a boolean true value only if both the left and right operators resolve to boolean true.这意味着只有当左右运算符都解析为布尔真时,它才会返回布尔真值。

& means a bitwise AND of the left and right operators. & 表示左右运算符的按位与。 This means the bits of each operand will be compared and the result will be the ANDed value, not a boolean.这意味着将比较每个操作数的位,结果将是 AND 运算的值,而不是布尔值。 If you do 101 & 100 the return value is 100 .如果您执行101 & 100则返回值为100 If you do 1 & 0 , the return value is 0 .如果执行1 & 0 ,则返回值为0

You've been mislead about the meaning of the two operators if someone told you the difference was just in efficiency.如果有人告诉您区别仅在于效率,您就被误导了这两个运算符的含义。 They have very different uses.它们有非常不同的用途。

Your friend is wrong about C#.你的朋友对 C# 的看法是错误的。

C# does not mix logical and bitwise operators, so you cannot use & where && is needed or vice versa. C# 不混合逻辑运算符和按位运算符,因此您不能在需要 && 的地方使用 & ,反之亦然。

1 & 0 returns 0 1 & 0 返回 0

true && false returns false true && false 返回 false

So if you're writing an if statement, which expects a boolean, you have to use &&.因此,如果您正在编写一个需要布尔值的 if 语句,则必须使用 &&。 And if you're doing bit-wise arithmetic, then you need &.如果你在做按位算术,那么你需要 &。

Yes.是的。 Javascript has both. Javascript 两者都有。 http://www.eecs.umich.edu/~bartlett/jsops.html http://www.eecs.umich.edu/~bartlett/jsops.html

Exactly as is true in C#, the double && version can stop as soon as it encounters a false, and the single & version may not.与 C# 中的 true 完全一样,双 && 版本可以在遇到 false 时立即停止,而单 & 版本可能不会。

You can check this for bitwise in javascript:您可以在 javascript 中按位检查:

https://www.w3schools.com/js/js_bitwise.asp https://www.w3schools.com/js/js_bitwise.asp

& is Sets each bit to 1 if both bits are 1 & is 如果两位都为 1,则将每一位设置为 1

Examples: 5 & 1 = 1例子:5 & 1 = 1

0101 & 0001 = 0001 0101 & 0001 = 0001

&& is the logical operator in Javascript. && 是 Javascript 中的逻辑运算符。 1 && 0 should return false so it is performing correctly. 1 && 0 应该返回 false 以便它正确执行。

Javascript has the bitwise (&) and boolean (&&) operators. Javascript 具有按位 (&) 和布尔 (&&) 运算符。 The reason && returns a 0 on 1 && 0 is because 0 would indicate false and so 1 (true) && 0 (false) returns a false as both operators must evaluate to true to return a true && 在 1 && 0 上返回 0 的原因是因为 0 表示 false,因此 1 (true) && 0 (false) 返回 false,因为两个运算符都必须评估为 true 才能返回 true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM