0

While making something, I accidentally wrote

const today = Date()

Instead of

const today = new Date()

And was surprised to find that this is valid syntax, and today's type is a string. Moreover, Date() === new Date().toString() returns true.

I could not find anything in the MDN docs about this, why does this happens?

Jason
  • 632
  • 1
  • 5
  • 17

1 Answers1

2

MDN describes this behavior right below the top "Syntax" section:

Note: The only correct way to instantiate a new Date object is by using the new operator. If you simply call the Date object directly, such as now = Date(), the returned value is a string rather than a Date object.

It's described in the specification here:

  1. If NewTarget is undefined, then

    a. Let now be the Number that is the time value (UTC) identifying the current time.

    b. Return ToDateString(now).

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320