Why Does "True" == True Show False in JavaScript

Why does true == true show false in JavaScript?

Because "true" is converted to NaN, while true is converted to 1. So they differ.

Like you reported, both are converted to numbers, because at least true can be (see Erik Reppen's comment), and then compared.

Why does true == 'true' statement in JS return false?

Because they don't represent equally convertible types/values. The conversion used by == is much more complex than a simple toBoolean conversion used by if ('true').

So given this code true == 'true', it finds this:

"If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y."

So you see it starts by becoming ToNumber(true) == 'true', which is 1 == 'true', and then tries again, where it now finds:

If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).

So now it's doing 1 == ToNumber('true'), which is 1 == NaN, which of course is false.

Why do both [] == true and ![] == true evaluate to false?

It's the way coercion works.

The first step of coercion is to convert any non primitive types to primitive types, then using a set of rules convert the left, right or both sides to the same type. You can find these rules here.

In your case [] == true, would pass through these 4 steps:

  1. [] == true
  2. [] == 1
  3. "" == 1
  4. 0 == 1

Whereas based on operator precedence the ! in ![] == true is executed first so the expression is converted to false == true which is obviously false.

You can try the live demo by Felix Kling to better understand how the sameness operator works.

In more words:

The value ![] is false, because [] is an Object (arrays are objects) and all objects, not including null, are truthy. So any array, even if it is empty will always be a truthy, and the opposite of a truthy is always false. The easiest way to check if a value is a truthy is by using !!.

console.log("![]: " + ![]);
console.log("!![]: " + !![]);

Why is [] == ![] true in JavaScript?

Type conversion in JS, particularly with regards to loose equality, is a tricky beast.

The best place to always start when answering the question "why does this particular loose equality evaluate this way" is to consult this table of equality comparisons by operand type.

In this case, we can see that for [] == false, Operand A is an Object and Operand B is a Boolean, so the actual comparison performed is going to be ToPrimitive(A) == ToNumber(B).

The right side of that is simple; ToNumber(false) evaluates to 0. Done and done.

The left side is more complex; you can check the official ECMAScript spec for full documentation of ToPrimitive, but all you really need to know is that in this case it boils down to A.valueOf().toString(), which in the case of the empty array is simply the empty string ""

So, we end up evaluating the equality "" == 0. A String/Number == comparison performs ToNumber on the string, and ToNumber("") is 0, so we get 0 == 0, which is of course true.

Javascript true +/- true is false?

The general solution for this in javascript is to use !! to parse to a boolean. !! negates the truthiness twice, resulting in a boolean which has the same truthiness of the original.

You should then use && as a logical and operation.

var a=null;
var b=true;
console.log(!!a && !!b); // false

Edit: An addendum on the strange + behaviour

The strangeness you're seeing when using + instead of && is because, in JavaScript, + coerces booleans to integers, with true becoming 1 and false becoming 0.

Hence

true + true \\ 2
true + false \\ 1

And then when doing

true + true == true

the left-hand-side of the equality comparison resolves to 2, JavaScript then coerces the right-hand-side to 1 and thus the equality check fails.

When doing

null + true == true

the left-hand-side becomes the integer 1, and then so does the right.

I'd recommend reading the MDN guide on Equality comparisons and sameness for more on JavaScript's value coercion and abstract equality checks.

If ([] == false) is true, why does ([] || true) result in []?

Type conversion is not related to falsy and truthy values.

What is truthy and what is falsy is defined by the ToBoolean function defined in the specs and [] is indeed truthy.

On the other hand, [] == false returns true because of the type conversion that happens during the evaluation of the expression.

The rules of type conversion say that for x == y

If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

ToNumber results in 0 for false so we're left with the evaluation of [] == 0. According to the same rules

If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y.

ToPrimitive results in an empty string. Now we have "" == 0. Back to our type conversion rules

If Type(x) is String and Type(y) is Number,
return the result of the comparison ToNumber(x) == y.

ToNumber results in 0 for "" so the final evaluation is 0 == 0 and that is true!

Why javascript comparison (true == true) is false?

Question has been asked before here.

In essence, "true" is converted to NaN, while true is converted to 1 (which is a boolean. Hence, they differ.

Why the boolean false is less than boolean true?

It's because false evaluates to 0, and true evaluates to 1. Because 1 > 0, true > false, and vice versa.



Related Topics



Leave a reply



Submit