8

According to the ECMA script standard, the following code should return true, but it doesn't:

d = new Date() ;
d.setTime(1436497200000) ;
alert( d == 1436497200000 ) ;

Section 11.9.3 says:

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

Then, section 8.12.8 says that ToPrimitive retuns the result of the valueOf method. Which means that the last line in my example above should be equivalent to:

alert( d.valueOf() == 1436497200000 );

Which does return true, indeed.

Why does the first case not return true?

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
GetFree
  • 40,278
  • 18
  • 77
  • 104
  • 1
    the 1st one compares an object to a number as a string. you can use `alert( +d == 1436497200000)` to force a numerical comparison. – dandavis Jul 01 '15 at 19:55
  • The bit you quote talks about the type of *x*, which is the *left hand* operand. Which in your case is not a string or number. – nnnnnn Jul 01 '15 at 19:58
  • @brso05: That _"article"_ is a manual conversion of the _official_ EcmaScript standard. – Cerbrus Jul 01 '15 at 20:03
  • @dandavis, ok, but why it doesn't work as the specification says? or am I reading it wrong? – GetFree Jul 01 '15 at 20:03
  • "When the [[DefaultValue]] internal method of O is called with no hint, then it behaves as if the hint were Number, *unless O is a Date object* (see 15.9.6), in which case it behaves as if the hint were String." - So when I try `alert(d == "Fri Jul 10 2015 13:00:00 GMT+1000 (AUS Eastern Standard Time)")` it returns `true`. – nnnnnn Jul 01 '15 at 20:04
  • @GetFree: you are reading it wrong. it is confusing. cerbrus is spot on. – dandavis Jul 01 '15 at 20:22

2 Answers2

5

If you look at the spec at section 8.12.8, you will find this text near the end of the section:

When the [[DefaultValue]] internal method of O is called with no hint, then it behaves as if the hint were Number, unless O is a Date object (see 15.9.6), in which case it behaves as if the hint were String.

(Emphasis mine)

Now, in step 8 / 9 of the The Abstract Equality Comparison Algorithm [11.9.3], ToPrimitive(x) and ToPrimitive(y) are called without hint parameter.

The lack of this hint parameter, together with the above text, means that the ToPrimitive method returns the toString() value, on date objects.

As you're probably aware, (new Date()).toString() returns a string representation of the date in American English [source]:
"Wed Jul 01 2015 22:08:41 GMT+0200 (W. Europe Daylight Time)"

That a string like that doesn't equal 1436497200000 shouldn't come as a big surprise. ;-)

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • 2
    so `toPrimitive` is given no hint in this case, even though it's been compared to a number. – GetFree Jul 01 '15 at 20:13
  • 1
    Yes, as you can see in steps 8 / 9 in section [11.9.3](http://ecma262-5.com/ELS5_HTML.htm#Section_11.9.3), `ToPrimitive(x)` and `ToPrimitive(y)` are called without `hint` parameter. (Contrary to how it's called in section [11.8.5](http://ecma262-5.com/ELS5_HTML.htm#Section_11.8.5)) – Cerbrus Jul 01 '15 at 20:19
0

ToPrimitive(A) attempts to convert its object argument to a primitive value, by attempting to invoke varying sequences of A.toString and A.valueOf methods on A.

So if the toString() call succeeds, it won't call valueOf().

Nathan Wilson
  • 856
  • 5
  • 12