Date Coercion in Arithmetic
You'll learn why Date objects behave differently with + versus other arithmetic operators.
JavaScript's Date objects have special coercion behavior that can surprise you. When you use the unary + operator on a date, you get a numeric timestamp. But when you use a date in other arithmetic contexts, things get interesting.
The Dual Nature of Date Coercion
A Date object can convert to either a string or a number, depending on the "hint" JavaScript uses:
+new Date()
// 1736025600000 (numeric timestamp)
new Date() + 1
// "Fri Jan 05 2025 12:00:00 GMT-0500 (EST)1" (string concatenation!)
Why the Difference?
The unary + operator explicitly requests numeric coercion using the ToNumber abstract operation. A Date responds by returning its timestamp (milliseconds since January 1, 1970).
But the binary + operator has a dual nature (remember from earlier lessons?). When it sees a Date object, it invokes ToPrimitive with no specific hint (or "default" hint). For Date objects specifically, the "default" hint is treated as "string", so the date converts to a string first. Then + performs string concatenation.
Other Arithmetic Operators
Operators like -, *, / always demand numeric coercion:
new Date() - 0
// 1736025600000 (numeric timestamp)
new Date() * 1
// 1736025600000 (numeric timestamp)
Key Takeaway: Date objects default to string coercion with the + operator but numeric coercion with unary + or other arithmetic operators, due to JavaScript's ToPrimitive hint preferences.