0

I came across the fact, that when you compare dates in JavaScript, the following happens:

$ new Date() > null
> true

$ new Date() > undefined
> false

I was wondering, what exactly happens under the hood, that leads to this behavior.

Finn Poppinga
  • 269
  • 4
  • 11
  • Automatic type coercion. `null` is coerced to `0`, `undefinend` is coerced to `NaN` before comparison. – Teemu Sep 11 '17 at 08:46
  • This is not possible. You just can not assign it to null – khan Sep 11 '17 at 08:51
  • @Teemu dont you agree with me? Date can not and should not be assigned null. if you dont agree you can search it. – khan Sep 11 '17 at 08:53
  • 1
    @khan Umh ... There's no assignments in the OP's code. And JS is a weakly typed language ... – Teemu Sep 11 '17 at 08:54
  • @Teemu maybe you are right Js is weakly typed language but still you cant just null the date. Yes there is not assignments but what the code says ? null the date ! right? My point is null for Date is not gonna show you any error warnings neither any result other then undefined. – khan Sep 11 '17 at 09:05
  • 3
    @khan The code doesn't "null the date", it just creates a new date, and __compares__ it to __`null`__ and to __`undefined`__, no assignments, no nullifying, just date objects and automatic type coercion of `null` and `undefined`. – Teemu Sep 11 '17 at 09:17
  • @Teemu I understand what you are saying I must admit you are quite right. But I still dont understand comparing null to date and undefined does not make any sense why would developer do that even though the out come is pretty much obvious. – khan Sep 11 '17 at 09:52
  • 4
    @khan Probably not on purpose, but in a case a date is compared against a variable which contains `null` or `undefined`, it is good to know what the result is going to be. – Teemu Sep 11 '17 at 10:05
  • 1
    Thank you very much for sharing your knowledge @Teemu this is the first time I learned something from deleted question LOL. – khan Sep 11 '17 at 11:04

0 Answers0