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.


What is the output of the for-in loop iterating over the object person? const person = { name: 'Lydia', age: 21 }; for (const item in person) { console.log(item); }

  1. { name: "Lydia" }, { age: 21 }

  2. "name", "age"

  3. "Lydia", 21

  4. ["name", "Lydia"], ["age", 21]

The correct answer is: "name", "age"

In the given question, the for-in loop is used to iterate over the properties (keys) of the object `person`. When using the for-in statement, it retrieves the keys of the object, not the values associated with those keys. In this case, the object `person` has two keys: `name` and `age`. As the loop executes, it will first assign `item` to the first key, which is `name`, and will log this to the console. In the second iteration, `item` is assigned to `age`, and this is also logged. Thus, the output of the loop will produce exactly the keys of the object, which are the strings "name" and "age". This aligns precisely with the correct choice, which indicates these strings as the output of the loop. The other options suggest outputs that either combine keys with values or format the output differently, which does not reflect the functionality of the for-in loop in this context.