Accessing Object Properties with Variables -Real-World usage

Tell us what’s happening:
I just wanted to know why would some use brackets to access the value of a property within a variable rather than use dot notation when you have access to the object. Basically, I’m asking about real-world usage or examples of using this approach.

Your code so far


// Setup
var testObj = {
  12: "Namath",
  16: "Montana",
  19: "Unitas"
};

// Only change code below this line;

var playerNumber;       // Change this Line
var player = testObj;   // Change this Line


var dogs = {
  Fido: "Mutt", Hunter: "Doberman", Snoopie: "Beagle"
};
var myDog = "Hunter";
var myBreed = dogs[Hunter];
console.log(myDog); // "Doberman"

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables

Your code actually doesn’t work, you should do dogs[myDog], what you’re doing there is accessing the property using a variable, which you can’t do if you use object notation. If you don’t need a variable to access the property, then, in the real-world, it’s preferred to use object notation.

If your object key has a space or other special characters in it, its value can’t be accessed with dot notation. So, if in your program you’re building an object dynamically and can’t be sure that no key contains space, it’s better to use bracket notation.