4

I'm learning about == vs. === and came across this answer which was very helpful in understanding the concept. However I wondered about one of the examples:

'0' == false     // true

This might make sense, since == doesn't check for type. But then I tried some possible coercions in the console and found the following:

Boolean('0')     // true
String(false)    // "false"

I would have thought '0' == false has the same truth value as '0' === String(false), but that doesn't seem to be the case.

So how does the coercion actually work? Is there a more basic type I'm missing?

Community
  • 1
  • 1
Lucy Bain
  • 2,496
  • 7
  • 30
  • 45
  • String(false) is returning the String representation of false, whilst Boolean('0') is converting the string '0' into true (as a non empty string is truthy) – atmd Dec 17 '14 at 10:50
  • 1
    Answers to all your questions you can find here: http://www.ecma-international.org/ecma-262/5.1/ A little hard to read at first but you will benefit a lot, if you spend some time reading through. – dfsq Dec 17 '14 at 10:52
  • Some [possibly surprising examples of `==`](https://stackoverflow.com/a/47015438/199364). – ToolmakerSteve Oct 27 '20 at 19:42

2 Answers2

3

"0" is a string containing the character 0, it is not the numeric value 0. The only string-type value which evaluates to false is "".

"0" is truthy.

Section 9.2 of the ECMAScript 262 specification defines how different types are converted to Boolean:

Argument Type   Result
Undefined       false
Null            false
Boolean         The result equals the input argument (no conversion).
Number          The result is false if the argument is +0, −0, or NaN; otherwise the
                result is true.
String          The result is false if the argument is the empty String (its length is
                zero); otherwise the result is true.
Object          true

This, however, is only strictly followed when comparing using ===.

When using Boolean('0') you're converting the value '0' to Boolean (which is the same as using !!'0'). When loosely comparing '0' with false, the Boolean value is converted to a number (as defined here). false, when converted to a number, becomes 0. This means the final calculation is '0' == 0 which equates to true.

To summarise the relevant part of the linked section of the ECMAScript specification above:

  1. Let x = '0' and y = false.
  2. Check if the type of y is Boolean.
  3. If true, convert y to a number.
  4. Compare x to the numeric equivalent of y.

In our case, a JavaScript implementation of this would be:

var x = '0',                      // x = "0"
    y = false;                    // y = false

if (typeof y === "boolean") {
    y = +y;                       // y = 0
}

console.log( x == y );            // "0" == 0
-> true
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
  • Sorry, I'm still confused. If "0" is truthy, how can `'0' == false` be true? – Lucy Bain Dec 17 '14 at 10:56
  • 1
    See this answer: http://stackoverflow.com/questions/7615214/in-javascript-why-is-0-equal-to-false-but-not-false-by-itself – James Donnelly Dec 17 '14 at 11:01
  • Thanks @james-donnelly! That link was exactly what I was looking for, but didn't know it :) – Lucy Bain Dec 17 '14 at 11:03
  • @LucyBain I've added a little update to my answer which will hopefully make everything clear. The key part of the ECMAScript specification is http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3, specifically points 6 and 7. – James Donnelly Dec 17 '14 at 11:07
  • @LucyBain slow day at work, so I've also added a JavaScript method of how to apply the ECMAScript's definition of how to compare using `==`. – James Donnelly Dec 17 '14 at 11:14
  • Thanks so much, that update made it really clear! And the specification was really helpful - I thought it took the type from the first operand. TIL :) – Lucy Bain Dec 17 '14 at 11:20
0

To make things more confusing for totally new in programming world:

Boolean('false')
true

Boolean('true')
true

I think it is easier and more intuitive to use !! operator for some reason. I dont know if i make sense, but i never used Boolean()

Answering the question, i found that thread useful: Difference between == and === in JavaScript

Community
  • 1
  • 1
Pavelloz
  • 551
  • 5
  • 12
  • that SO question is where I got my example from, but I couldn't figure out how it worked. I'll stick with `===` for now :) – Lucy Bain Dec 17 '14 at 11:21