Example 1 — Basic toJSON() Output
Call toJSON() on a UTC instant — same shape as toISOString().
const event = new Date("August 19, 1975 23:15:30 UTC");
console.log(event.toJSON());
// "1975-08-19T23:15:30.000Z"
console.log(event.toISOString());
// "1975-08-19T23:15:30.000Z" How It Works
- Build a Date. The string includes
UTC, so the instant is unambiguous. - Call toJSON. Returns the portable UTC ISO string ending in
Z. - Compare toISOString. For valid dates, both methods return the same text.
- No side effects. The
Dateis unchanged; only a string is returned.

