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 questionsThe 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]'.
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.
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.
Keep practicing
Take the free JS-Dev-101 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other JS-Dev-101 practice questions
- Question 1A developer executes: document.cookie; document.cookie = 'key=John Smith'; What is the be…
- Question 2A developer is required to write a function that calculates the sum of elements in an arr…
- Question 3Given the code below: 01 const delay = async delay => { 02 return new Promise((resolve, r…
- Question 401 function Animal(size, type) { 02 this.type = type || 'Animal'; 03 this.canTalk = false…
- Question 5Refer to the code snippet: 01 let array = [1, 2, 3, 4, 4, 5, 4, 4]; 02 for (let i = 0; i …
- Question 6Code: 01 let array = [1, 2, 3, 4, 4, 5, 4, 4]; 02 for (let i = 0; i < array.length; i++) …
