Why is it possible to subtract Date objects in javascript? Is there any form of operator overloading?
date, javascript, operator-keyword, overloading
Solution
The operator `-` converts the operands to numbers (check for example `"12"-3`). The date object defines a numeric conversion `.valueOf()` that returns the number of milliseconds.
See also for example `+(new Date)`.
Problem
Why is the below code actually working? Code ``` var firstDate = new Date(); // some time passing here var secondDate = new Date(); // Difference seems to contain difference in miliseconds. var difference = secondDate - firstDate; ``` What I get is, I believe, an equivalent to `secondDate.getTime() - firstDate.getTime()`. The only question is, how can this conversion to number of milliseconds happen in background? Is this some sort of operator-overloading?