Reputation: 32103
in JavaScript, 0 && 1
evaluates to 0
, which is the lower of the two. Why, then, does 0.1 && 1
evaluate to 1
, which is the higher of the two?
Similarly, why does 0 || 1
evaluate to 1
, but 0.1 || 1
evaluate to 0.1
Upvotes: 1
Views: 91
Reputation: 20667
You're specifically discussing logical operators, which actually do not care about the numbers themselves. Your code could be rewritten as
0 && 1 - false && true
0.1 && 1 - true && true
0 || 1 - false || true
0.1 || 1 - true || true
I assume you're setting this to a variable like x
or something. When you set it, the variable is being assigned as the first portion of the test that "fully" passes.
So, your cases above work like this:
Since this has nothing to do with the size of the numbers, just whether or not they are 0 or not 0, you can actually show this more clearly with using something like the following:
0 && -1 - false && true
0.1 && -2 - true && true
0 || -3 - false || true
0.1 || -4 - true || true
Upvotes: 0
Reputation: 19635
There are several different things going on:
0 is bitwise false
, and any number other than 0 evaluates to true
, so the expression 0 && 1
evaluates to false && true
, which is of course false
.
1 is bitwise true
, and as per above any number <> 0 also evaluates to true
, so 0.1 && 1
evaluates to true && true
, which is true
.
Using the information above:
0 || 1
evaluates to false || true
, which is true
The final example is perhaps the most interesting one, and it has to do with operator short-circuiting.
The logical or operator (||
) short-circuits, or stops evaluating, as soon as it encounters a value that evaluates to true
. Thus, if you are using logical or in an assignment operation, it will return the first true
value in the expression.
Thus, 0.1 || 1
returns 0.1. But, if you were to evaluate 1 || 0.1
, it would instead return 1.
As a related aside, the logical and operator (&&
) will short-circuit as soon as it encounters a value that evaluates to false
.
Upvotes: 0
Reputation: 61
You should check this out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators.
Basically the && will take the first of the two if it is falsey otherwise it will take the second.
The opposite is true for ||.
Upvotes: 1
Reputation: 179086
The &&
and ||
operators do not return values based on inequalities such as <
or >
.
a && b
works as:
if (a) {
return b;
}
else {
return a;
}
a || b
works as:
if (a) {
return a;
}
else {
return b;
}
The if
statements are based on the concept of "truthy" and "falsey" values, where 0
, NaN
, null
, undefined
, ''
, and false
are all "falsey". All other values are "truthy".
0 && 1
evaluates to 0
because 0
is falsey.
0.1 && 1
evaluates to 1
because 0.1
is truthy.
Upvotes: 0
Reputation: 245429
It has nothing to do with which value is larger, the operators will return the appropriate value for the spec.
In the case of &&
if the first parameter is false, it will be returned. Otherwise the second is returned. In your example 0.1 && 1
, 0.1
is a truth-y value so 1
is returned. You could just as easily try 100000000 && 0.1
and see that 0.1
is returned. The reason that 0 && 1
returns 0
is because 0
is false-y so, per the spec, the first value gets returned.
Likewise, with ||
if the first parameter is true, it will be returned. Otherwise the second is returned.
Upvotes: 8