19
new Date(null)
// Date 1970-01-01T00:00:00.000Z

How come when I type new Date(null) in JavaScript console I'm getting
Date 1970-01-01T00:00:00.000Z?

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
Baikuntha
  • 211
  • 1
  • 2
  • 7

3 Answers3

33

Because the ECMAScript 2017 standard says so?

The end of ECMA section 20.3.1.1 claims:

The exact moment of midnight at the beginning of 01 January, 1970 UTC is represented by the value +0.

...which might make you say, "...but!, but!, I did not say +0, I said":

new Date(null)

Ok, so let's follow the standard on that one...

That Date constructor example goes to section 20.3.2.2, where item 3.b.iii there says:

3.b.iii: Else, let V be ToNumber(v).

ToNumber is a hyperlink, so follow that to section 7.1.3 where there is a number conversion table that shows:

Argument Type  | Result
Null                       | +0

Therefore:

new Date(null)

Effectively becomes:

new Date(+0)

Which is why you ultimately get:

Date 1970-01-01T00:00:00.000Z

Paul T.
  • 4,703
  • 11
  • 25
  • 29
2

Apparently because that (Unix timestamp 0) is what a Date object is initialized to by default, eh?

bipll
  • 11,747
  • 1
  • 18
  • 32
0

On doing new Date(null), it returns Wed Dec 31 1969 19:00:00 GMT-0500 (Eastern Standard Time) for me.

Siddharth Jain
  • 380
  • 1
  • 3
  • 16
  • 1
    This is 1/1/1970 at midnight in UTC time, which is the Unix epoch. This means your result is the same as in the question, accounting for timezone differences. – Oliver Nov 11 '20 at 22:38