logoalt Hacker News

publicdebatesyesterday at 7:02 PM3 repliesview on HN

Guessed 2 of the first 3 questions.

Got to question 4 and gave up:

    new Date("not a date")
    1) Invalid Date
    2) undefined
    3) Throws an error
    4) null
There's literally no way of guessing this crap. It's all random.

Replies

dvtyesterday at 9:17 PM

I had no idea we even had an `Invalid Date` object, that's legitimately insane. Some other fun ones:

    new Date(Math.E)
    new Date(-1)
are both valid dates lol.
winstonpyesterday at 7:15 PM

the new Date() constructor is an amalgamation of like 5 different specs, and unless the input matches one of them, which one kicks in is up to the implementer's choice

marcosdumayyesterday at 7:30 PM

The choice here is really surprising. I was half-expecting NaN, that you omitted.

Is there any other instance of the standard JS library returning an error object instead of throwing one? I can't think of any.

show 2 replies