LearnFrenzy
Sign in Take a free test

Objects · Predict the Output · Question 13

JSON.stringify with special values Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 13 Back to Exercise

What does the following JavaScript code print (console output, as in Node.js)?

main.js
console.log(JSON.stringify({ a: undefined, b: () => 1, c: NaN, d: null, e: [undefined, () => 1] }));
  • A {"a":undefined,"c":NaN,"d":null,"e":[]}
  • B {"c":"NaN","d":null,"e":[null]}
  • C {"c":null,"d":null,"e":[null,null]} Correct
  • D {"a":null,"b":null,"c":null,"d":null,"e":[null,null]}
Correct Answer: Option C

undefined and function properties are dropped, NaN becomes null, and inside arrays undefined and functions become null so the indexes stay correct.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links