2018-09-30 23:01:58 +01:00
---
id: 56533eb9ac21ba0edf2244c9
title: Accessing Object Properties with Variables
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cnQyKur'
2019-07-31 11:32:23 -07:00
forumTopicId: 16165
2021-01-13 03:31:00 +01:00
dashedName: accessing-object-properties-with-variables
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-09-30 23:01:58 +01:00
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.
2020-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
Here is an example of using a variable to access a property:
2019-05-17 06:20:30 -07:00
```js
var dogs = {
Fido: "Mutt", Hunter: "Doberman", Snoopie: "Beagle"
};
var myDog = "Hunter";
var myBreed = dogs[myDog];
console.log(myBreed); // "Doberman"
```
2018-09-30 23:01:58 +01:00
Another way you can use this concept is when the property's name is collected dynamically during the program execution, as follows:
2019-05-17 06:20:30 -07:00
```js
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"
```
2020-11-27 19:02:05 +01:00
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--
Set the `playerNumber` variable to `16` . Then, use the variable to look up the player's name and assign it to `player` .
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`playerNumber` should be a number
```js
assert(typeof playerNumber === 'number');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
The variable `player` should be a string
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof player === 'string');
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
The value of `player` should be "Montana"
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(player === 'Montana');
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
You should use bracket notation to access `testObj`
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(/testObj\s*?\[.*?\]/.test(code));
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
You should not assign the value `Montana` to the variable `player` directly.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(!code.match(/player\s*=\s*"|\'\s*Montana\s*"|\'\s*;/gi));
```
You should be using the variable `playerNumber` in your bracket notation
```js
assert(/testObj\s*?\[\s*playerNumber\s*\]/.test(code));
```
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --after-user-code--
2018-09-30 23:01:58 +01:00
```js
2018-10-20 21:02:47 +03:00
if(typeof player !== "undefined"){(function(v){return v;})(player);}
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
## --seed-contents--
```js
// Setup
var testObj = {
12: "Namath",
16: "Montana",
19: "Unitas"
};
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
// Only change code below this line
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
var playerNumber; // Change this line
var player = testObj; // Change this line
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
var testObj = {
12: "Namath",
16: "Montana",
19: "Unitas"
};
var playerNumber = 16;
var player = testObj[playerNumber];
```