和&之间的区别是什么?和&&在JavaScript? [英] What's the difference between & and && in JavaScript?

查看:192
本文介绍了和&之间的区别是什么?和&&在JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和&之间的区别是什么?和&&在JavaScript中?

What's the difference between & and && in JavaScript?

示例代码:

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 &?

(顺便说一句,&&&&和Python似乎是&和;&在Python中。)

(By the way, && seems to be "and" in Python; & seems to be & in Python.)

推荐答案

& 是按位AND



此运算符需要两个数字并返回数字。如果它们不是数字,则会转换为数字。

& is bitwise AND

This operator expects two numbers and retuns a number. In case they are not numbers, they are cast to numbers.

它是如何工作的?维基百科有一个答案: https://en.wikipedia.org/wiki/ Bitwise_operation#AND

How does it work? Wikipedia has an answer: https://en.wikipedia.org/wiki/Bitwise_operation#AND

注意:在Javascript中,此操作符的使用不鼓励,因为没有整数数据类型,只是浮点数。因此,浮动在每次操作之前都会转换为整数,从而使其变慢。此外,它在典型的Web应用程序中没有实际用途,并且产生无法读取的代码。

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.

一般规则:避免。不要使用它。很少有可维护和可读的JS代码。

General rule: Avoid. Don't use it. It rarely has place in a maintainable and readable JS code.

它需要两个参数并返回:

It expects two arguments and returns:


  • 评估为假的第一学期

  • 否则为上学期(如果全部为真-y)

以下是一些例子:

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)

如果你只在布尔上使用它,这正是来自数学逻辑的AND算子。

If you only ever use it on Boolean, this is exactly the AND operator from mathematical logic.

此运算符定义如上的原因是运算符链接。您可以链接此运营商并仍然遵守上述规则。

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)



& & 短路



从定义中可以看出,一旦你发现一个术语是假y,你就不需要了不关心以下条款。 Javascript甚至更进一步,这些术语甚至没有评估。这称为短路。

&& 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.

true && false && alert("I am quiet!")

此声明不会提醒任何事情和 false 返回。因此,您可以使用&& 运算符作为if语句的较短替换。这些是等价的:

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:

if (user.isLoggedIn()) alert("Hello!")
user.isLoggedIn() && alert("Hello!")

几乎所有的JS压缩器都使用这个技巧来节省2个字节。

Almost all JS compressors use this trick to save 2 bytes.

这篇关于和&之间的区别是什么?和&&在JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆