SyntaxStudy
Sign Up
JavaScript JSON Handling of null and undefined
JavaScript Intermediate 4 min read

JSON Handling of null and undefined

null vs undefined in JSON

JSON supports null but not undefined. Properties with undefined values are omitted by stringify; undefined in arrays becomes null.

Example
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]"
Pro Tip

Design your API to use null for "intentionally absent" values and omit fields for "not applicable" — be consistent throughout the codebase.