Serialization Edge Cases
You'll learn: How JSON.stringify handles tricky JavaScript values that don't play nicely with JSON format.
Values JSON Can't Handle
JSON.stringify works great with objects, arrays, strings, numbers, booleans, and null. But some JavaScript values cannot be represented in JSON text, so they receive special (sometimes surprising) treatment:
Functions Disappear
Functions are executable code, not data. JSON can't store them, so JSON.stringify silently removes function properties from objects:
const obj = {
name: "Alice",
greet: function() { return "Hi!"; }
};
JSON.stringify(obj); // '{"name":"Alice"}'
// The greet method vanished!
Undefined Also Vanishes
undefined is a JavaScript-specific concept with no JSON equivalent. It gets removed from objects and converted to null in arrays:
JSON.stringify({ a: undefined }); // '{}'
JSON.stringify([1, undefined, 3]); // '[1,null,3]'
Symbols Are Ignored
Symbols (property keys or values) are skipped entirely:
JSON.stringify({ [Symbol("id")]: 123 }); // '{}'
Circular References Throw Errors
If an object references itself (directly or through a chain), JSON.stringify throws an error because JSON cannot represent circular structures:
const circular = {};
circular.self = circular;
JSON.stringify(circular); // ❌ TypeError: Converting circular structure
Key Takeaway: JSON.stringify omits undefined, functions, and symbols from objects; converts undefined to null in arrays; and throws errors on circular references—know these edge cases when serializing complex objects.