Common JSON Mistake. Why JSON.stringify Ignores Undefined but Keeps Null JavaScript Deep Dive. Why Undefined Disappears in JSON.stringify but Null Stays

Common JSON Mistake. Why JSON.stringify Ignores Undefined but Keeps Null JavaScript Deep Dive. Why Undefined Disappears in JSON.stringify but Null Stays

Lets explain the Code:

let x = {
a: undefined, b: null } console.log(JSON.stringify(x));

Step 1: Object Declaration

let x = {
a: undefined, b: null }

What happens here:

  • You are creating an object x with two properties:
    • Property a is explicitly assigned undefined.
    • Property b is explicitly assigned null.

Quick property breakdown:

  • undefined

    • Represents the absence of a value.
    • Often appears when a variable or object property has been declared but not assigned a value.
  • null
    • Represents the intentional absence of any object value.
    • It is explicitly set by the developer to indicate "no value" or "empty."

Step 2: JSON.stringify(x)

console.log(JSON.stringify(x));

What is JSON.stringify?

It is a built-in JavaScript method that converts a JavaScript object or value into a JSON string.

Key Rule:

  • When you convert an object to JSON:

    • Properties with undefined are excluded from the final string.
    • Properties with null are included.
Why?

According to the JSON specification:

  • JSON supports:
    • Numbers
    • Strings
    • Booleans
    • Arrays
    • Objects
    • null
  • JSON does not support undefined as a valid value.

How JSON.stringify Works in This Case:

Original object:

{
a: undefined, b: null }

After applying JSON.stringify(x):

'{"b":null}'

Explanation:

  • Property a is ignored because its value is undefined (not serializable in JSON).
  • Property b is included because null is a valid JSON value.


✔ Final Answer:

B) {"b":null}

This question appeared in the Epam 2nd round of interview, testing the candidate’s knowledge of Json data type convert object into Json vise-versa in JavaScript.


Why Does This Happen?

  • JSON.stringify skips:
    • undefined values
    • Function properties
    • Symbol properties
  • JSON.stringify includes:

    • Numbers
    • Strings
    • Booleans
    • Objects
    • Arrays
    • null

Key Notes:

Value TypeBehavior in JSON.stringify
undefinedSkipped
nullIncluded
FunctionSkipped
SymbolSkipped

Example Extension:

let obj = {
a: undefined, b: null, c: 42, d: "Hello" } console.log(JSON.stringify(obj)); // Output: {"b":null,"c":42,"d":"Hello"}

The undefined property is skipped, but null, numbers, and strings are included.


Final Summary:

StepActionResult
1Declare object with undefined and nullObject created
2Apply JSON.stringifyundefined skipped
3Print JSON string{"b":null}

Key Takeaways:

  • undefined is not valid in JSON and is excluded from the JSON string.
  • null is valid in JSON and is included.
  • JSON.stringify creates cleaner JSON by removing non-serializable values automatically.


Post a Comment

Previous Post Next Post