|
| ▲ | dvt 7 hours ago | parent | next [-] |
| 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. |
|
| ▲ | winstonp 10 hours ago | parent | prev | next [-] |
| 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 |
|
| ▲ | marcosdumay 9 hours ago | parent | prev [-] |
| 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. |
| |
| ▲ | jazzyjackson 7 hours ago | parent | next [-] | | I think NaN itself is a bit of an error object, especially in how it's passed through subsequent math functions, which is a different choice than throwing up. But besides that I think you're right, Invalid Date is pretty weird and I somehow never ran into it. One consequence is you can still call Date methods on the invalid date object and then you get NaN from the numeric results. | |
| ▲ | WorldMaker 3 hours ago | parent | prev [-] | | The fun trick is that Invalid Date is still a Date: > let invalid = new Date('not a date')
> invalid
Invalid Date
> invalid instanceof Date
true
You were half-correct on expecting NaN, it's the low level storage of Invalid Date: > invalid.getTime()
NaN
Invalid Date is just a Date with the "Unix epoch timestamp" of NaN. It also follows NaN comparison logic: > invalid === new Date(NaN)
false
It's an interesting curio directly related to NaN. |
|