0
alert (0 == ''); // true
alert (0 == '0'); // true

JSFiddle Proof

I understand that == in javascript performs a conversion and then checks for equality, but how does it perform the conversion in the statements above? Does it convert 0 to '' or '' to 0? Or something else perhaps? Is there a spec somewhere that explains the implementation?

Rick
  • 1,391
  • 8
  • 11
NibblyPig
  • 51,118
  • 72
  • 200
  • 356
  • Also, see http://stackoverflow.com/questions/1995113/strangest-language-feature/1998224#1998224 – Matt Sep 09 '11 at 16:08

3 Answers3

5

It uses the Abstract Equality Comparison Algorithm.

Specifically for your example

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

So then you'll end up with:

0 == 0

...in both cases because an empty string converts to the number 0, and a numeric string converts to the number given.

From 9.3.1 ToNumber Applied to the String Type:

A StringNumericLiteral that is empty or contains only white space is converted to +0

Since we're now doing a comparison of the same Type on the second pass, it will do the following:

If Type(x) is the same as Type(y), then

... If Type(x) is Number, then

... If x is the same Number value as y, return true.


To test a toNumber conversion, you can use the unary + operator.

console.log( +'' );   // 0
console.log( +'0' );  // 0
user113716
  • 318,772
  • 63
  • 451
  • 440
1

The specification is ECMAScript.

The algorithm is given in 11.9.3, The Abstract Equality Comparison Algorithm.

For both, "Type(x) is Number and Type(y) is String, [] return the result of the comparison x == ToNumber(y)"

ToNumber is in 9.3. MV stands for "mathematical value." I think the rules that apply are:

  • "The MV of StringNumericLiteral ::: [empty] is 0."
  • "The MV of StrNumericLiteral ::: StrDecimalLiteral is the MV of StrDecimalLiteral"

In other words, an empty string converts to 0, and '0' is just a regular decimal in string form.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • You've specified the section, so it isn't as big a deal, but there is also an [Abstract Relational Comparison Algorithm](http://es5.github.com/#x11.8.5), so it's a good idea to include *Equality* in the title. – user113716 Sep 09 '11 at 15:48
  • @patrick, thanks. That's what I get for not copy and pasting. – Matthew Flaschen Sep 09 '11 at 15:52
  • Even though the down-vote came when the answer only consisted of the first sentence, in fairness, even then it did answer the final question that OP was asking *"Is there a spec somewhere that explains the implementation?"*. +1 to offset and for the update. – user113716 Sep 09 '11 at 16:23
1

How this is converted is covered in section 11.9.3 in the EMCA 262 spec. The following rule is applicable to this scenario (where x == y)

if Type(x) is Number and Type(y) is String then convert y to a number and compare

In both examples here JavaScript will convert y to a number and both will result in 0 so the comparison is true

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454