JavaScript 中的 & 和 && 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7310109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between & and && in JavaScript?
提问by Martin Thoma
What's the difference between & and && in JavaScript?
JavaScript 中的 & 和 && 有什么区别?
Example-Code:
示例代码:
var first = 123;
var second = false;
var third = 456;
var fourth = "abc";
var fifth = true;
alert(first & second); // 0
alert(first & third); // 72
alert(first & fourth); // 0
alert(first & fifth); // 1
alert(first && second); // false
alert(first && third); // 456
alert(first && fourth); // abc
alert(first && fifth); // true
It seems like && is a logical "and" which gives me always the second value if both are true.
似乎 && 是一个逻辑“和”,如果两者都为真,它总是给我第二个值。
But what is &?
但什么是&?
(By the way, && seems to be "and" in Python; & seems to be & in Python.)
(顺便说一句,&& 在 Python 中似乎是“和”;& 在 Python 中似乎是 &。)
回答by Rok Kralj
&
is bitwise AND
&
按位与
This operator expects two numbersand retuns a number.In case they are not numbers, they are cast to numbers.
此运算符需要两个数字并返回一个数字。如果它们不是数字,则将它们转换为数字。
How does it work?Wikipedia has an answer: https://en.wikipedia.org/wiki/Bitwise_operation#AND
它是如何工作的?维基百科有一个答案:https: //en.wikipedia.org/wiki/Bitwise_operation#AND
Note:In Javascript, the usage of this operator is discouraged,since there's no integer data type, just floating point. Thus floats are converted to integers prior to every operation, making it slow. Also, it has no real use in typical web applications and produces unreadable code.
注意:在 Javascript 中,不鼓励使用此运算符,因为没有整数数据类型,只有浮点数。因此,在每次操作之前,浮点数都会转换为整数,从而使其变慢。此外,它在典型的 Web 应用程序中没有实际用途,并且会生成不可读的代码。
General rule: Avoid. Don't use it.It rarely has place in a maintainable and readable JS code.
一般规则:避免。不要使用它。它很少出现在可维护和可读的 JS 代码中。
&&
is logical AND
&&
是逻辑 AND
It expects two arguments and returns:
它需要两个参数并返回:
- First term that evaluates to false
- Last term otherwise (if all are true-y)
- 第一个评估为假的术语
- 否则最后一项(如果全部为真-y)
Here are some examples:
这里有些例子:
0 && false 0 (both are false-y, but 0 is the first)
true && false false (second one is false-y)
true && true true (both are true-y)
true && 20 20 (both are true-y)
If you only ever use it on Boolean, this is exactly the AND operator from mathematical logic.
如果您只在布尔值上使用它,那么这正是数学逻辑中的 AND 运算符。
&&
operator chaining
&&
操作符链接
The reason this operator is defined as above is operator chaining. You are able to chain this operator and still keep the above rules.
上面定义这个运算符的原因是运算符链接。您可以链接此运算符并仍然保留上述规则。
true && 20 && 0 && 100 0 (it is the first false-y)
10 && 20 && true && 100 100 (last one, since all are true-y)
&&
short-circuiting
&&
短路
As can be seen from the definition, as soon as you find that one term is false-y, you needn't to care about the following terms. Javascript even takes this a notch further, the terms are not even evaluated. This is called short circuiting.
从定义中可以看出,只要你发现一个术语是false-y,你就不必关心后面的术语。Javascript 甚至更进一步,甚至没有评估这些术语。这称为短路。
true && false && alert("I am quiet!")
This statement doesn't alert anything and false
is returned. Therefore, you could use the &&
operator as a shorter replacement for an if statement. These are equivalent:
此语句不会发出任何警告并false
返回。因此,您可以使用&&
运算符作为 if 语句的较短替代品。这些是等效的:
if (user.isLoggedIn()) alert("Hello!")
user.isLoggedIn() && alert("Hello!")
Almost all JS compressors use this trick to save 2 bytes.
几乎所有的 JS 压缩器都使用这个技巧来节省 2 个字节。
回答by duri
&
is the bitwise "and". This means that if you have two numbers converted to binary, the result is a number that has the 1
digit at the positions where both numbers have 1
.
&
是按位“与”。这意味着,如果您将两个数字转换为二进制,则结果是一个1
数字在两个数字都有1
.
100011 //35
& 111001 //57
---------
100001 //35 & 57 == 33
回答by GladHeart
With all the lofty, detailed insights throughout, the practical layman's answer that solved my head beating on an IF statement string of four chained && causing FAILURE to do the evaluating job at hand as an IF ((sessionStorage.myLable !== "LableStringA") && (sessionStorage.myLable !== "LableStringB") && (sessionStorage.myLableZ !== "LableStringA") && (sessionStorage.myLableZ !== "LableStringB")) { ...) was PRACTICALLY in the real world solved with the -3 down voted solution (now showing just below) by simply using a single & instead of && Thank you Jake Wayne (and Russ). Note, in testing my example != also worked if used in place of the prefered !==
凭借贯穿始终的所有崇高而详细的见解,实用的外行答案解决了我在四个链式 && 的 IF 语句字符串上跳动的问题,导致 FAILURE 作为 IF ((sessionStorage.myLable !== "LableStringA" ) && (sessionStorage.myLable !== "LableStringB") && (sessionStorage.myLableZ !== "LableStringA") && (sessionStorage.myLableZ !== "LableStringB")) { ...) 在现实世界中实际上已解决使用 -3 否决的解决方案(现在显示在下面),只需使用单个 & 而不是 && 谢谢 Jake Wayne(和 Russ)。请注意,在测试我的示例时 != 如果用于代替首选 !== 也有效
回答by Jake Wayne
To determine whether two boolean values put together are true or false, if you want to check them both (like validation on the web page), you may use the "&" operator. "&" is bitwise AND.
要确定两个布尔值放在一起是真还是假,如果您想同时检查它们(如网页上的验证),您可以使用“&”运算符。“&”是按位与。
With the "&&" operator, once it finds the first value is false, it will end evaluation and not to check the second value.
使用“&&”运算符,一旦发现第一个值是假的,它将结束评估而不检查第二个值。