null vs undefined in JSON
JSON supports null but not undefined. Properties with undefined values are omitted by stringify; undefined in arrays becomes null.
JSON supports null but not undefined. Properties with undefined values are omitted by stringify; undefined in arrays becomes null.
const obj = {
a: "hello",
b: null, // Included as null
c: undefined, // Omitted from output
d: function(){}, // Omitted (functions not in JSON)
};
JSON.stringify(obj);
// {"a":"hello","b":null}
// Undefined in arrays becomes null
JSON.stringify([1, undefined, 3]);
// "[1,null,3]"
Design your API to use null for "intentionally absent" values and omit fields for "not applicable" — be consistent throughout the codebase.
More in JavaScript