The second way to access a key’s value is by using bracket notation, [ ]
.
You’ve used bracket notation when indexing an array:
['A', 'B', 'C'][0]; // Returns 'A'
To use bracket notation to access an object’s property, we pass in the property name (key) as a string.
We must use bracket notation when accessing keys that have numbers, spaces, or special characters in them. Without bracket notation in these situations, our code would throw an error.
let spaceship = { 'Fuel Type': 'Turbo Fuel', 'Active Duty': true, homePlanet: 'Earth', numCrew: 5 }; spaceship['Active Duty']; // Returns true spaceship['Fuel Type']; // Returns 'Turbo Fuel' spaceship['numCrew']; // Returns 5 spaceship['!!!!!!!!!!!!!!!']; // Returns undefined
With bracket notation you can also use a variable inside the brackets to select the keys of an object. This can be especially helpful when working with functions:
let returnAnyProp = (objectName, propName) => objectName[propName]; returnAnyProp(spaceship, 'homePlanet'); // Returns 'Earth'
If we tried to write our returnAnyProp()
function with dot notation (objectName.propName
) the computer would look for a key of 'propName'
on our object and not the value of the propName
parameter.
Let’s get some practice using bracket notation to access properties!
Instructions
Let’s use bracket notation to access the value of 'Active Mission'
from the spaceship
object in the code editor. Create a variable isActive
and assign the spaceship
‘s 'Active Mission'
property to it.
Using bracket notation and the propName
variable provided, console.log()
the value of the 'Active Mission'
property.