Basic JavaScript - Accessing Object Properties with Variables

Tell us what’s happening:
My question isn’t regarding the problem of the exercise, but the example. Notice in the example, after the object is defined, the following entry:

const myDog = "Hunter";

The question is, how does this differ from assigning the string “Hunter” to this variable? How does JS know that we’re referencing a property of the object “dogs”?

BTW the code included is the example, not the exercise…

Your code so far

//Beginning of example

const dogs = {
  Fido: "Mutt",
  Hunter: "Doberman",
  Snoopie: "Beagle"
};

const myDog = "Hunter";
const myBreed = dogs[myDog];
console.log(myBreed);

// End of example

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Accessing Object Properties with Variables

Link to the challenge:

After considering this, I think I see what’s going on (but correct me if I’m wrong):

“Hunter” is a normal string, assigned to myDog, with no explicit ties to the object “dogs”. Its value is merely being invoked to target the “Doberman” value in the object “dogs”. The string is globally accessible as any other string. Correct?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.