Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

Why does JSON.stringify drop undefined and functions?

Do you know what will be the output of this code?

JavaScript
console.log(JSON.stringify({ a: undefined, b: null, c: () => 1 }));
Show answer and explanation

The answer is {"b":null}.

JSON supports null but not undefined or functions. Object properties with those values are left out. Inside arrays they become null instead.

Take the full quiz Next question →

More Objects questions

  1. Why do object keys 1 and "1" overwrite each other?
  2. What happens when you use an object as an object key?
  3. Why is {} === {} false in JavaScript?
  4. Are JavaScript objects copied by reference?
  5. Does the spread operator make a deep copy?

Related topics