Link Search Menu Expand Document

Accessing Object Properties with Variables

Summary

  • Bracket notation can be used to access a property which is stored as a value of a variable.

Final Code

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

// Only change code below this line

var playerNumber = 16;       // Change this line
var player = testObj[playerNumber];   // Change this line