FAQ: Objects - Looping Through Objects


This community-built FAQ covers the “Looping Through Objects” exercise from the lesson “Objects”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Looping Through Objects

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


What does the tt${}: (I replaced grave accent ` as tt to prevent markup) mean in console.log(tt${crewMember}: ${spaceship.crew[crewMember].name}tt)?


This code was used in the example in this exercise: for (let crewMember in spaceship.crew) {
console.log(${crewMember}: ${spaceship.crew[crewMember].name})
My question is, why do you use a bracket around crewMember in the last line? It is not a two-word string, nor does it have special characters or a space.??


because of JS, in JS objects and associative arrays are the same, so we can just mix them:

myObj = {
  a: 3
console.log(myObj.a); // dot nation
console.log(myObj['a']); // associative array notation

for the dot nation (object notation if you like), we must use the same of the property, for the associative array notation, the key needs to be string.

when using a for ... in loop, the iterator (crewMember in your case) is of type string (you could use typeof to verify this), in which case we have to use the associative array notation


Why isn’t it possible to use

console.log(crewMate + ": " + crewMate.name);


console.log(crewMate + ": " + crewMate['name']);

which is much shorter and much more readable?


because crewMate is not in the global namespace, its a nested object.


I still don’t understand. Practically it crewMate.name does ouputs undefined but why? If crewMate is already a variable then crewMate.name should be working.


the for in loop gives the properties back as string, so crewMate will contain strings. These strings do not have a name property


I’m puzzled. The examples introduce formatting/syntax that are new to me but go unexplained. Why are the backticks necessary in the console.log statement, and where do the dollar signs come from? (Maybe that has been explained in very early lessons that I finished a long time a go, but I’ve reviewed every lesson now that I’ve returned to learning javascript, star). Also: where does this dollar sign come from all of a sudden? Why is ‘crewMember’ surrounded with accolades at first and then with square brackets?
Update (while writing): after reviewing earlier lessons, it appears that these back ticks and dollar signs are explained in the ‘Variables’ lesson, at ‘8. String interpolation’. I don’t think they have made an appearance in any subsequent lesson until now. I may not be the first person who’s thrown off by their sudden reappearance.


${} is just a lot cleaner to insert variable in a string then using +:

let x = 5;
console.log("hello world " + x + " times");
console.log(`hello world ${x} times`);

for ${} to work, the string needs to be enclosed in backticks.


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

In the above code, why can we call ${crewMember} without needed to specify spaceship.crew[crewMember]? Why can we not use ${crewMember.name} in the second part of the console.log() string?


the for in loop will give strings:

for (let crewMember in spaceship.crew) {
   console.log(crewMember, typeof crewMember)

these strings do not have a name property

if you want the captain, you would do:


you can’t access captain directly, given its part of crew object, which in its turn is part of spaceship object


Why is this syntax is incorrect?:

for (let position in spaceship.crew){
console.log(position + ': ’ + spaceship.crew[position][name]);

Supposedly we can write the keys in bracket notation, right?

This one also returned error, but as far as I know, if the keys don’t have special characters, we can write them through dot notation:
for (let position in spaceship.crew){
console.log(position + ': ’ + spaceship.crew .position.name);


yes, but then the keys should be strings.


That makes sense. Thank you.

I still don’t get why dot notation didn’t work. Is it because dot notation can’t be used with the argument of the function?


because using the dot syntax, you need the property name:


if you have the propertyName as string, you have to use the associative array notation (with square brackets)


But none of my properties were strings. I’ll paste my code:

let spaceship = {
    crew: {
    captain: { 
        name: 'Lily', 
        degree: 'Computer Engineering', 
        cheerTeam() { console.log('You got this!') } 
    chiefOfficer: { 
        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!') } 

// Write your code below
for (let position in spaceship.crew){
  console.log(position + ': ' + spaceship.crew.position.name);

for (let position in spaceship.crew){
  console.log(spaceship.crew.position.name + ': ' + spaceship.crew.position.degree);

This returned ‘TypeError: Cannot read property ‘name’ of undefined’ at the dot after position unless I wrapped it with brackets.

Thank you for taking time to answer.


the for in loop gives a string:

for (let position in spaceship.crew){
   console.log(typeof position);


I thought that the loop had something to do but I didn’t know what. So just to be clear, then: variables on the for in loop are treated as strings, right?


the for in loop assigns the property names to the variable (position in your case).