What is the log output for the following code: console.log(JSON.stringify([new Number(3), new String('false'), new Boolean(false)]));

Prepare for the Salesforce JavaScript Developer Exam. Utilize comprehensive quizzes, flashcards, and multiple choice questions with hints and explanations. Boost your exam readiness!

The log output for the provided code is indeed a string with the value of "[3,"false",false]". This result comes from how the JSON.stringify method handles different object types in JavaScript.

When you pass the array containing instances of Number, String, and Boolean to JSON.stringify, it performs serialization on those objects. For instance:

  • The new Number(3) instance gets converted to the primitive value 3.
  • The new String('false') instance is converted to the primitive string value 'false'.
  • The new Boolean(false) instance converts to the primitive boolean value false.

The JSON.stringify method specifically only captures the primitive values of these wrapper objects when converting them to a JSON string format. Therefore, the output correctly reflects the conversion of those wrapped values into a JSON-compatible format, resulting in the string "[3,"false",false]".

This output illustrates an important aspect of JavaScript object behavior, particularly with wrapper types for primitives, and how JSON handles the serialization of these objects.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy