What is the difference between & and && in JavaScript?

What is the difference between and and && in 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 that && is a logical "and", which always gives me a second meaning, if both are true.

But what is &?

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

+47
javascript bitwise-operators logical-operators
Sep 05 '11 at 15:35
source share
3 answers

& bitwise value AND

This operator expects two numbers and reconfigures the number . . If they are not numbers, they are transferred to numbers.

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

Note. . In Javascript, the use of this operator is discouraged since there is no integer data type, only floating point. Thus, the floats are converted to integers before each operation, which slows down the work. In addition, it has no real use in typical web applications and creates unreadable code.

General Rule: Avoid. Do not use it. It rarely occurs in supported and readable JS code.

&& is logical AND

It expects two arguments and returns:

  • The first term that evaluates to false
  • The last term otherwise (if everything is true-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) 

This definition corresponds to the definition of a logical and mathematical field.

&& chain of operators

The reason this operator is defined above is because of the chain of operators. You can bind this operator and still adhere to 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 circuit

As you can see from the definition, once you find that one term is false-y, you do not need to worry about the following terms. Javascript even makes this more visible, the terms are not even evaluated. This is called a short circuit.

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

This statement does not warn anything and returns false . Therefore, you can use the && operator as a shorter replacement for the if statement. They are equivalent:

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

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

+83
Sep 05 '11 at 15:36
source share

& is the bitwise "and". This means that if you have two numbers converted to binary, the result is a number that has the number 1 in those positions where both numbers have 1 .

  100011 //35 & 111001 //57 --------- 100001 //35 & 57 == 33 
+16
Sep 05 '11 at 15:40
source share

To determine if two booleans match: true or false, if you want to check both of them (for example, check on a web page), you can use the "&" operator. "& Amp;" bitwise I.

With the "& &" operator, once it finds the first value, it will be false, it will complete the evaluation and will not check the second value.

-four
Sep 20 2018-12-12T00:
source share



All Articles