https://www.codecademy.com/courses/introduction-to-javascript/lessons/objects/exercises/for-in
for (let crewMember in spaceship.crew) {
console.log(${spaceship.crew[crewMember].name}: ${spaceship.crew[crewMember].degree}
)
};
};
in the example above there is an example that prints the obj name and degree properties , but how ? why is let not assigned a value?
for (let crewMember in spaceship.crew) {
console.log(`${spaceship.crew[crewMember].name}: ${spaceship.crew[crewMember].degree}`);
};
The for loop iterates over all the properties in spaceship.crew
and in each iteration, the property is assigned as a string to the loop variable crewMember
.
For the exercise, in the first iteration crewMember
will be assigned the string "captain"
. In the next iteration, crewMember
will be assigned the string "chief officer"
. Similarly, in the next iterations, the variable will be assigned the strings "medic"
and "translator"
.
As for the “Bracket Notation” for accessing values in objects, see this post:
https://discuss.codecademy.com/t/faq-objects-looping-through-objects/371716/177
If you still have doubts about the topic, share your thoughts on what you find confusing.
3 Likes