Iterating through an object in JavaScript

Anyone has a good explanation why we can’t use the dot notation instead of the bracket notation on the last line of this code to console log the crew Member’s name?

console.log(${crewMember}: ${});

instead of:

console.log(${crewMember}: ${spaceship.crew[crewMember].name});

I can’t see the reason why not.

let spaceship = {
  crew: {
    captain: { 
      name: 'Lily', 
      degree: 'Computer Engineering', 
    'chief officer': { 
      name: 'Dan', 
      degree: 'Aerospace Engineering', 
    medic: { 
      name: 'Clementine', 
      degree: 'Physics', 
    translator: {
      name: 'Shauna', 
      degree: 'Conservation Science', 

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

I think because in the loop, crewMember takes on different values. If you want to access values of objects using a variable that holds the value for the key, you have to use squared brackets.

To access “Lily” you could say:




const jobType = "captain"