Sub topic 3
Immutable by Design
This chapter assumes you know const and let, how JavaScript passes object references, and how strict mode works. It also assumes that Temporal is available, either natively or through the polyfill set up in an earlier chapter.
The problem with mutable dates
A legacy Date is mutable: its setter methods change the object in place. Because objects are passed by reference, any code holding the same reference sees that change.
const invoiceDate = new Date('2024-03-15T12:00:00Z');
const reminder = invoiceDate; // same object, not a copy
reminder.setUTCDate(reminder.getUTCDate() + 5);
console.log(invoiceDate.toISOString()); // '2024-03-20T12:00:00.000Z'The invoice date moved even though the code only meant to compute a reminder. This bug is hard to find when the shared reference crosses function or module boundaries. A common defense is to copy with new Date(d) before every change, but that relies on every caller remembering to do it.
How Temporal behaves instead
Every Temporal object is immutable. Methods such as add, subtract, with, and round never change the receiver. They return a new object.
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
const invoiceDate = Temporal.PlainDate.from('2024-03-15');
const reminder = invoiceDate.add({ days: 5 });
const nextYear = invoiceDate.with({ year: 2025 });
console.log(invoiceDate.toString()); // '2024-03-15'
console.log(reminder.toString()); // '2024-03-20'
console.log(nextYear.toString()); // '2025-03-15'Since no operation can change a value, you can pass Temporal objects into functions, store them in caches, or share them between modules without making defensive copies.
Fields are read-only
Properties like year, month, and day are getters with no setters. In strict mode, which includes all ES modules, assigning to one throws a TypeError. In sloppy-mode scripts the assignment fails silently.
const d = Temporal.PlainDate.from('2024-03-15');
try {
d.day = 20;
} catch (e) {
console.log(e instanceof TypeError); // true
}
console.log(d.day); // 15To change a field, use with. It returns a new object with the fields you name replaced.
Common error: discarding the result
People moving from Date often call a method and expect the variable to update:
let deadline = Temporal.PlainDate.from('2024-03-15');
deadline.add({ weeks: 1 }); // result thrown away
console.log(deadline.toString()); // '2024-03-15'
deadline = deadline.add({ weeks: 1 }); // reassign the variable
console.log(deadline.toString()); // '2024-03-22'Here let lets the variable point to a new value. The value itself never changes. Declaring the variable with const only prevents reassignment. Immutability comes from Temporal itself.
| Operation | `Date` | Temporal |
|---|---|---|
| Change a field | `d.setMonth(5)` mutates `d` | `d.with({ month: 6 })` returns a new value |
| Add time | `d.setDate(d.getDate() + 1)` | `d.add({ days: 1 })` |
| Safe to share | No, copy first | Yes |
Exercise
After running const a = Temporal.PlainDate.from('2024-01-10'); const b = a.subtract({ days: 3 }); a.with({ month: 2 });, what do a.toString() and b.toString() print, and why?
Show answer
a prints '2024-01-10' and b prints '2024-01-07'. The subtract call returns a new value, which is stored in b. The with call also returns a new value, but nothing stores it, so it is lost. Neither call changes a.
Exercise
A function extendTrial(start) receives a PlainDate and must return a date 14 days later without affecting the caller's value. Write it.
Show answer
function extendTrial(start) { return start.add({ days: 14 }); } No copy is needed, because add never modifies start.