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

Practice this question and more.


When logging the types in the following code: let s_prim = 'foo'; let s_obj = new String(s_prim); what will the output be?

  1. String, String

  2. Object, String

  3. String, Object

  4. Object, Object

The correct answer is: String, Object

In the given code snippet, `let s_prim = 'foo';` defines a primitive string, while `let s_obj = new String(s_prim);` creates a String object using the primitive string as the initial value. When you log the types of `s_prim` and `s_obj`, the `typeof` operator is used in JavaScript, which determines the type of a variable: 1. For `s_prim`, since it is a primitive string, using `typeof s_prim` will return `"string"`. 2. For `s_obj`, since it is an instance of the String object created with the `new` keyword, using `typeof s_obj` will return `"object"`. Thus, when you log the types of both variables, the output will indicate that the first is a primitive string and the second is an object type created from the String constructor. This results in the output being "String, Object," signifying the expected types produced by the `typeof` operator for each variable.