this post was submitted on 12 Jul 2025
145 points (98.7% liked)

Programmer Humor

37243 readers
78 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 6 years ago
MODERATORS
 

cross-posted from: https://programming.dev/post/33754840

new Date("wtf")

you are viewing a single comment's thread
view the rest of the comments
[–] mogoh@lemmy.ml 4 points 6 days ago (8 children)

Ok, I get that the Date API is problematic, but I wouldn't expect anything meaningfull from new Date("not a date").getTime() anyway. Why would you in the first place?

[–] Ephera@lemmy.ml 7 points 5 days ago

It's mainly horrid, because it means you have to code extremely defensively (or I guess, use a different API).
You can't rely on new Date("not a date") aborting execution of your function by throwing an error. Instead, you have to know that it can produce an Invalid Date object and check for that. Otherwise a random NaN shows up during execution, which is gonna be extremely fun to try to find the source of.

I understand that it's implemented like that partially for historical reasons, partially because it's often better to display "NaN" rather than nothing, but it's still the sort of behavior that puts me in a cold sweat, because I should be memorizing all kinds of Best Practices™ before trying to code JavaScript.

load more comments (7 replies)