Sub topic 4
What Happens to undefined, Functions, Symbols, NaN, and Dates
This chapter assumes you already know how JSON.stringify turns objects and arrays into text, and how JSON's data types differ from JavaScript's. Here we look at what happens when a value has no direct JSON equivalent.
JSON supports strings, numbers, booleans, null, objects, and arrays. JavaScript has more types than that. JSON.stringify doesn't throw on most of the extra ones. It drops them or converts them quietly, so data can go missing without any error.
The Three Rules
Where an unsupported value sits decides what stringify does with it:
| Value | As an object property | As an array element | At the top level |
|---|---|---|---|
| `undefined` | property omitted | `null` | returns `undefined` |
| function | property omitted | `null` | returns `undefined` |
| symbol | property omitted | `null` | returns `undefined` |
| `NaN`, `Infinity`, `-Infinity` | `null` | `null` | `"null"` |
| valid `Date` | ISO string | ISO string | ISO string (quoted) |
Arrays turn these values into null because removing an element would shift every later index. Objects can drop a property without moving anything else.
const data = {
a: undefined,
b: () => 1,
c: Symbol("s"),
d: NaN,
e: new Date(0)
};
console.log(JSON.stringify(data));
// {"d":null,"e":"1970-01-01T00:00:00.000Z"}
console.log(JSON.stringify([undefined, () => 1, Symbol("s")]));
// [null,null,null]
console.log(JSON.stringify(undefined)); // undefined (not a string)Non-Finite Numbers Become null
JSON numbers must be finite, so NaN, Infinity, and -Infinity all become null. Negative zero becomes 0. A parsed value won't match the original:
const stats = { ratio: 0 / 0, max: Infinity, offset: -0 };
const back = JSON.parse(JSON.stringify(stats));
console.log(back); // { ratio: null, max: null, offset: 0 }
console.log(typeof back.ratio); // "object" (null), not "number"Code that later runs Number.isNaN(back.ratio) gets false, because the value is now null.
Dates Become Strings
A Date has a built-in toJSON method that returns toISOString(), so it is written as a string such as "1970-01-01T00:00:00.000Z". An invalid date writes null. JSON.parse does not turn these strings back into Date objects. They stay strings until you convert them yourself, which a later chapter covers with revivers.
const event = { at: new Date(Date.UTC(2024, 0, 15)), bad: new Date("nope") };
const text = JSON.stringify(event);
console.log(text);
// {"at":"2024-01-15T00:00:00.000Z","bad":null}
const parsed = JSON.parse(text);
console.log(parsed.at instanceof Date); // false
console.log(typeof parsed.at); // "string"Common Mistakes
- Treating missing as undefined. When
{ status: undefined }is sent to a server, it arrives as{}. If the server reads "key absent" differently from "key set to null", usenullto clear a field on purpose. - Calling
.lengthon the result.JSON.stringify(maybeUndefined)can returnundefinedinstead of a string, so.lengthor.trim()then throws aTypeError. - Losing symbol keys.
stringifyalso skips properties whose keys are symbols, not only those whose values are symbols. - Comparing dates after a round trip.
parsed.at > other.atcompares strings, andparsed.at.getTime()throws.
const id = Symbol("id");
const obj = { [id]: 1, name: "x" };
console.log(JSON.stringify(obj)); // {"name":"x"}Exercise
What does JSON.stringify({ a: [undefined, NaN, 1], b: undefined, c: () => 0, d: new Date(0) }) return?
Show answer
{"a":[null,null,1],"d":"1970-01-01T00:00:00.000Z"}. Inside the array, undefined and NaN become null. The properties b and c are omitted because their values are undefined and a function. The Date d is written as its ISO string.
Exercise
A function runs const body = JSON.stringify(payload); send(body.length); and sometimes crashes with "Cannot read properties of undefined". What kind of payload causes this, and how would you guard against it?
Show answer
The crash happens when payload is undefined, a function, or a symbol. In those cases JSON.stringify returns undefined instead of a string. Guard with a check such as if (typeof body !== "string") throw new Error("payload not serializable"), or default the payload to null so stringify returns "null".