JavaScript JSON.stringify(): Serialization Rules
Quick answer
JSON.stringify converts supported JavaScript data into JSON text. Some JavaScript values disappear or change representation, so serialization is not an exact object snapshot.
How it works
JSON.stringify(value, replacer, space) visits serializable own enumerable string-keyed properties and array elements. Undefined, functions and symbols in objects are omitted; in arrays they become null. Non-finite Numbers become null. A top-level unsupported value can return undefined rather than a string. BigInt normally throws unless explicitly converted. toJSON can customize representation, as Date does.
console.log(JSON.stringify({a:undefined,b:null,c:NaN}));Expected output in Node.js:
{"b":null,"c":null}Undefined object property is omitted, NaN becomes null.
console.log(JSON.stringify([undefined,Infinity,3]), typeof JSON.stringify(undefined));Expected output in Node.js:
[null,null,3] undefinedArray positions remain, while unsupported root values yield undefined.
Common mistakes
Do not use JSON.stringify as a general deep equality operation. Property order, omissions, custom serialization and cycles complicate it.
Practice
Serialize a record containing a numeric value and optional null; decide which missing fields should be explicit.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript JSON.stringify(): Serialization Rules — Knowledge Check
Apply the taught concept and predict the output.
What happens to an ordinary enumerable object property containing undefined during JSON serialization?
What does JSON.stringify normally do with a raw BigInt value?
Which argument controls indentation in JSON.stringify?
What is the exact console output in JavaScript JSON.stringify(): Serialization Rules example 1?
console.log(JSON.stringify({a:undefined,b:null,c:NaN}));What is the exact console output in JavaScript JSON.stringify(): Serialization Rules example 2?
console.log(JSON.stringify([undefined,Infinity,3]), typeof JSON.stringify(undefined));