About NaN is most known that he is not equal to himself.
NaN === NaN
And that operations, impossible arithmetically, will be returned to NaN.
'BlaBlaBla'/0
But NaN has one little-known (?), And, as it seems to me, a very useful application.
TL; DR It's All About the Date
In a nutshell:
')
new Date(NaN)
What is useful? Invalid Date is still Date. And all operations with Date are still in place.
Any operations with Date other than directly setting the timestamp will return NaN, leaving the Date as an Invalid Date.
const x = new Date(NaN)
At the same time, checking for the validity of the date is nowhere easier
const x = new Date(NaN)
Note that the conversion to the timestamp is not required here, valueOf () does it under the hood.
All operations with Date are mutable. However, cloning through the designer works great with Invalid Date.
const x = new Date(NaN)
Comparing two dates directly in Date is not implemented and dates can only be compared via timestamp. NaN guarantees that the Invalid Date will definitely not be equal to any other date. I think this is a very useful feature.
const x = new Date(NaN)
To my regret, the Date constructor behaves somewhat strangely with respect to the input parameter.
new Date(null)
It would be much more logical to design Invalid Date, because null is not quite zero. Let's leave it to Javascript conscience.
However, if forcibly transferred to the constructor undefined, the result looks expected. So be careful.
new Date(undefined)
The article turned out more about Date than about NaN, but, in general, I wanted to tell about this bundle.