FAQ: Objects - Looping Through Objects


I think that because dot notations looks for Keys? and keys only?


i think that, what does that refer to? You use it, but without context its difficult to determine what you mean


I think the reason why you have to put [position] inside bracket notations is because it is a variable and not a property right?


close, the variable contains a string value, and when dealing with strings you have to use the associative array notation:

variable = 'key'
// or directly with a string:


but why do you have to put spaceship.crew again I thought varName was already defined as being spaceship.crew

for(let varName in spaceship.crew){
console.log(${varName}: ${spaceship.crew[varName].name})};

why cant I just put this ??
for(let varName in spaceship.crew){
console.log(${varName}: ${[varName].name})


because varName contains just a string, that is all it is:

for(let varName in spaceship.crew){

so now we need to use this string to access something inside the object


for(let crewMember in spaceship.crew){
console.log(crewMember+’: '+spaceship.crew[crewMember].name)
when we are using the for loop in this way, why can’t i creat spaceship.crew.crewMember.name instead the one I put in the for loop?

for(let prop in spaceship.crew.captain){

This for loop will out put three undefined

for(let prop in spaceship.crew.captain){
while this will output the three correct properties of the captian


Considering that crewMember is the current object, would you not be able to write,




oh, thanks. I find the anwser on top


if it is just a string why does varName work in the first curly brackets? I just don’t understand why you have to put spaceshp.crew[varName].name why not just put spaceship.crew.name then in this case instead of having to declare spaceship.crew to the vaiable varName


if we look at the crew object:

captain: { 
        name: 'Lily', 
        degree: 'Computer Engineering', 
        cheerTeam() { console.log('You got this!') } 
    'chief officer': { 
        name: 'Dan', 
        degree: 'Aerospace Engineering', 
        agree() { console.log('I agree, captain!') } 
    medic: { 
        name: 'Clementine', 
        degree: 'Physics', 
        announce() { console.log(`Jets on!`) } },
    translator: {
        name: 'Shauna', 
        degree: 'Conservation Science', 
        powerFuel() { console.log('The tank is full!') } 

i don’t see crew have a name property. crew has crew members (captain, medic and so on) which we are looping over, which is why we need varName (crewMember would be a better variable name)


I kind of understand better but not completely. why do we need varName or crewMember ? and also I still don’t understand why we have to put spaceship.crew[crewMember].name instead of just [crewMember].name ?

for example why cant we just put console.log({crewMember}: {crewMember.name});


This I would like to know also. for example why cant we just put console.log( {crewMember}: {crewMember.name} );


you see one works and the other dont! I am asking why do we need to put the spaceship.crew before? and why does the first varName work but not the second one?




because we write a loop:

for (let crewMember in spaceship.crew) {

JS will now assign each property of the crew object to crewMember variable.

because crewMember just contains a string, we need to use this string to access the current crew member.


what does string mean? I thought a string is words within quotations


String is a data type. Words are quite common, but a string can also be numbers:

console.log(typeof '1');

or symbols:

console.log(typeof '-+');


so basically it is to access all the objects within the crew object by creating the crewMember. it seems like we are making a fake name to get into there


yes, the for .. in loop is doing that for us. It assigns the properties in crew object to the for loop iterator (crewMember) as string.


thank you for your patience and help. this was a tough one to understand!