JS-Dev-101 practice question 11 of 25

Given the code: const copy = JSON.stringify([new String('false'), new Boolean(false), undefined]); What is the value of copy?

Full set: 148 questions
Answer options
A: '["false", false, null]' Correct answer
B: '[false, {}]'
C: '["false", false, undefined]'
D: '["false", {}]'
Correct Answer:
A. '["false", false, null]'
Explanation

The JSON.stringify() method converts a JavaScript object or value to a JSON string. Its behavior with special values is key here. It converts a String object wrapper (new String('false')) to its primitive string value, resulting in "false". It converts a Boolean object wrapper (new Boolean(false)) to its primitive boolean value, false. Crucially, when JSON.stringify() encounters undefined as a value within an array, it converts it to null. Therefore, the array is serialized to the string '["false",false,null]'.

Why Incorrect

B. This is incorrect because new Boolean(false) serializes to the primitive false, not an empty object {}, and undefined becomes null.
C. This is incorrect because JSON.stringify() converts undefined values inside an array to null, not the string "undefined".
D. This is incorrect because new Boolean(false) serializes to the primitive false, not an empty object {}, and it omits the undefined value entirely.

References

1. Mozilla Developer Network (MDN). JSON.stringify(). Section: "Description". It states: "If undefined, a Function, or a Symbol is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array)."

2. ECMAScript® 2015 Language Specification. 24.3.2 JSON.stringify(value [, replacer [, space]]). The internal JA(value) abstract operation for arrays specifies that if a value is undefined, the string null is returned.

3. Mozilla Developer Network (MDN). JSON.stringify(). Section: "Examples", "Trying to stringify a BigInt value". This section shows how wrapper objects like new Number() are converted to their primitive values, which applies to new String() and new Boolean() as well.

Scroll to Top