Pergunta

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

Foi útil?

Solução

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.

Outras dicas

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

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.

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.

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:

  1. The result is 0 because the test fails at 0 (0 && anything will fail).
  2. The result is 1 because the test "fully" passes after checking the right-hand side of the equation. It checks that 0.1 is "truthy" and then checks that 1 is "truthy" and returns 1 since that was the last piece checked.
  3. The result is 1 because the boolean logic checks 0 first, and then says "or 1". The "or 1" piece passes, so it returns 1.
  4. The result is 0.1 because that is "truthy" and it doesn't need to check the second side of the equation.

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top