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