1
Boolean("a")

returns true in the Browser console.
So why

"a" == true

returns false?

  • because per the spec, any non-zero value given to the Boolean constructor is considered truthy, but for equality comparison "a' is definitely not the same value as true – Mike Corcoran Aug 09 '17 at 13:50
  • I find it is illogical but I'll deal with it. –  Aug 09 '17 at 13:51
  • 1
    Possible duplicate of [In JavaScript, why is "0" equal to false, but when tested by 'if' it is not false by itself?](https://stackoverflow.com/questions/7615214/in-javascript-why-is-0-equal-to-false-but-when-tested-by-if-it-is-not-fals) – Ivar Aug 09 '17 at 13:53
  • 1
    @str `"true" == true` returns `false`. – Spencer Wieczorek Aug 09 '17 at 13:53
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness – epascarello Aug 09 '17 at 14:00

3 Answers3

1

How the == operator functions on certain types is defined in the ECMAScript specifications. It is as followed:

7.2.13 Abstract Equality Comparison

The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

  1. If Type(x) is the same as Type(y), then Return the result of performing Strict Equality Comparison x === y.
  2. If x is null and y is undefined, return true.
  3. If x is undefined and y is null, return true.
  4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ! ToNumber(y).
  5. If Type(x) is String and Type(y) is Number, return the result of the comparison ! ToNumber(x) == y.
  6. If Type(x) is Boolean, return the result of the comparison ! ToNumber(x) == y.
  7. If Type(y) is Boolean, return the result of the comparison x == ! ToNumber(y).
  8. If Type(x) is either String, Number, or Symbol and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
  9. If Type(x) is Object and Type(y) is either String, Number, or Symbol, return the result of the comparison ToPrimitive(x) == y.
  10. Return false.

Now we can apply them to this the cases above. Which first converts the boolean into a number and then attempts to convert the string into a number (which is parsed as NaN):

"a" == true
// Case 7 (true --> 1)
// =>"a" == 1
// Case 5 ("a" --> NaN)
// => NaN == 1
=> false
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
0

You need to see how Javascripts works.

"a" is a string. true is a boolean.

So "a" is not equal to true.

Doing Boolean("a"), is like to ask "Is "a" ?". This bizarre question will answer true if what you put in (so here "a") is [implicitly something not null or empty].

You shouldn't use this Boolean function in this context, the result will never be what you think (Boolean("false") will return true for example)

See here for more information: https://stackoverflow.com/a/264037/6532640

NatNgs
  • 874
  • 14
  • 25
  • 2
    The `===` operator is not the same as `==`. Using `==` will attempt to cast the types to make them the same. For example `"1" == true` returns `true`. – Spencer Wieczorek Aug 09 '17 at 13:56
  • Yeah, forgot to talk about this; but yeah `"1"==true` is `true` ( `"a"==true` is `false`, comparing values), when `"1"===true` will be `false` (comparing values and types to say simple) – NatNgs Aug 09 '17 at 13:58
0

// Boolean("a") is equal to asking if "a" == "a" so it will return true
console.log(Boolean("a"));

// "a" == true can't return true, because a string and a boolean can't be equal
console.log("a" == true);

// for the same reason this will return false too
console.log("true" == true);

// and this will return true
console.log("true" == true.toString());

//-----------------------------------------

// If we talk about numbers, there are different rules.
// In javascript you will be able to convert a string with 
// numbers to a number and vice versa 

// this is why "1" is equal to 1
console.log("1" == 1);

// considering the interest in trying to use every kind of
// variable in var, they described some basical rules
// for example adding a number to a string will work
// like every language

// this is why here you will obtain "11" as result
var one = "1";
console.log(one+1);

// in this case - doesn't work with strings, so our var one
// will be considered as an integer and the result will be 1
var one = "1";
console.log(one-1+1);
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32