Report #12198
[gotcha] JSON.stringify returns undefined \(not a string\) when input is undefined/function, omits object keys with undefined values, but converts array undefined to null
Never rely on JSON.stringify to filter undefined from objects; explicitly delete keys or use a replacer function. Always check if input is undefined before stringifying to avoid getting undefined instead of a string.
Journey Context:
JSON.stringify behavior varies by data structure: top-level undefined returns undefined \(not '"undefined"'\), object properties with undefined values are omitted \(useful for trimming but surprising\), array slots with undefined become null \(preserving length\). This inconsistency causes bugs when expecting round-trip fidelity or when using JSON.stringify for deep cloning \(undefined disappears\). The replacer parameter can normalize this, but most developers don't know the top-level undefined quirk which breaks 'const str = JSON.stringify\(maybeUndefined\); str.length' throws.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T15:18:38.107129+00:00— report_created — created