Accessing Object Properties with Variables Dot Vs Bracket Notation

Tell us what’s happening:
Hi just wanted to ask why does this not work when you use dot notation accessing objects properties with variables.

can you also please provide some reading material around this if possible.

  **Your code so far**

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

// Only change code below this line
const playerNumber = 16;  // Change this line
const player = testObj.[playerNumber];   // Change this line
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Safari/605.1.15

Challenge: Accessing Object Properties with Variables

Link to the challenge:

Well, this is using a dot, but this isn’t using dot notation for objects. This is a syntax error (you should be seeing this error in the console pane). The reason it doesn’t work is because it is not a valid use of the language. You can’t do this because it is not allowed.

If you are using a variable to look up a property in an object then you will not be able to use dot notation, you must use brackets. If you google “javascript dot vs bracket notation” you will get a ton of links to articles explaining the difference. Searching this forum for “dot vs bracket” will also give you plenty of links to previous discussions about this.

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