--- id: 56533eb9ac21ba0edf2244c9 title: Accessing Object Properties with Variables challengeType: 1 guideUrl: 'https://guide.freecodecamp.org/certificates/accessing-objects-properties-with-variables' --- ## Description
Another use of bracket notation on objects is to access a property which is stored as the value of a variable. This can be very useful for iterating through an object's properties or when accessing a lookup table. Here is an example of using a variable to access a property:
var dogs = {
  Fido: "Mutt", Hunter: "Doberman", Snoopie: "Beagle"
};
var myDog = "Hunter";
var myBreed = dogs[myDog];
console.log(myBreed); // "Doberman"
Another way you can use this concept is when the property's name is collected dynamically during the program execution, as follows:
var someObj = {
  propName: "John"
};
function propPrefix(str) {
  var s = "prop";
  return s + str;
}
var someProp = propPrefix("Name"); // someProp now holds the value 'propName'
console.log(someObj[someProp]); // "John"
Note that we do not use quotes around the variable name when using it to access the property because we are using the value of the variable, not the name.
## Instructions
Use the playerNumber variable to look up player 16 in testObj using bracket notation. Then assign that name to the player variable.
## Tests
```yml - text: playerNumber should be a number testString: 'assert(typeof playerNumber === ''number'', ''playerNumber should be a number'');' - text: The variable player should be a string testString: 'assert(typeof player === ''string'', ''The variable player should be a string'');' - text: The value of player should be "Montana" testString: 'assert(player === ''Montana'', ''The value of player should be "Montana"'');' - text: You should use bracket notation to access testObj testString: 'assert(/testObj\s*?\[.*?\]/.test(code),''You should use bracket notation to access testObj'');' - text: You should not assign the value Montana to the variable player directly. testString: 'assert(!code.match(/player\s*=\s*"|\''\s*Montana\s*"|\''\s*;/gi),''You should not assign the value Montana to the variable player directly.'');' - text: You should be using the variable playerNumber in your bracket notation testString: 'assert(/testObj\s*?\[\s*playerNumber\s*\]/.test(code),''You should be using the variable playerNumber in your bracket notation'');' ```
## Challenge Seed
```js // 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 ```
### After Test
```js console.info('after the test'); ```
## Solution
```js var testObj = { 12: "Namath", 16: "Montana", 19: "Unitas" }; var playerNumber = 16; var player = testObj[playerNumber]; ```