Almost everything is already said, but just for completeness' sake I want to take a look at the performance aspect (which you said doesn't matter, but it very well might):
JavaScript has a lot of difficult-to-remember rules on how to evaluate expressions. This includes a lot of type casting (implicit type coercion) when it comes to more complex comparisons. Arrays and Objects need to be converted by calling their toString()
methods and are then cast to numbers. This results in a huge performance hit.
The logical operator &&
is short-circuiting. This means as soon as it encounters a falsy value, the evaluation stops and false
is returned. The bitwise operator will always evaluate the entire statement.
Consider the following (yes, quite extreme) short circuit example when very expensive operations (casting an array and an object) are involved: ( performance according to https://jsbench.me in Chromium 90)
// logical operator
( false && {} && [] ) == true
// /\ short circuits here
// performance: 805M ops/sec
// bitwise operator
( false & {} & [] ) == true // evaluates the entire statement
// performance: 3.7M ops/sec
You can see that the performance differs by a factor of 100!
true
andfalse
value. The performance problem will kick in when you replace with real function. – nhahtdh&
keeps evaluating all the options while&&
stops at the firstfalse
, thus not evaluating the other conditions. – npinti&
with+
or*
. That doesn't mean+
or*
always do the same as the boolean&&
. – georg