JS Built-ins
← Date120 / 120
DatevalueOfintermediate

valueOf() β€” used in arithmetic

πŸ“– Usage Example
const d = new Date('2024-01-15T00:00:00.000Z')
d.valueOf()  // β†’ 1705276800000
d.valueOf() === d.getTime()  // β†’ true

Use valueOf() to get the millisecond timestamp of a Date; it equals getTime() and is called automatically in arithmetic expressions.

Date.prototype.valueOf()

When you subtract two Dates, JavaScript calls valueOf() automatically, returning the difference in ms.

Challenge: Verify that subtracting two Dates gives a number (the difference in ms).

πŸ’‘ Hints (1)
  • β€’ Date subtraction implicitly calls valueOf() on both operands, yielding the ms difference