Javascript Developer JSE 40 01 · Free Practice Question Easy
Question 18
Q423 - Control Flow
Examine the following code:
- let car = {make: "Citroen", model: "DS"};
- for (let f in car) console.log(f);
What will appear in the console as a result?
-
A
- "make" "model"
-
B
- "make: Citroen" "model: DS"
-
C
- "Citroen" "DS"
-
D
- "car"
Reveal correct answer
Correct answer: A
Explanation
Topic: for in over object
Try it yourself:
- let car = {make: "Citroen", model: "DS"};
- for (let f in car) console.log(f); // make model
Explanation:
The for in statements combo iterates over the properties of an object.
The code block inside the loop is executed once for each property.
https://www.w3schools.com/jsref/jsref_forin.asp
(There are similar questions Q323 and Q523.)
Q423 (Please refer to this number, if you want to write me about this question.)
A. The code uses a for...in loop to iterate over the properties of the 'car' object. It logs the property names ('make' and 'model') to the console, not the property values.
B. This choice is incorrect because the code is logging the property names ('make' and 'model') of the 'car' object individually, not in the format of "property: value".
C. This choice is incorrect because the code is logging the property names ('make' and 'model') of the 'car' object, not the property values ('Citroen' and 'DS').
D. This choice is incorrect because the code is iterating over the properties of the 'car' object and logging the property names ('make' and 'model'), not the object itself.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
