Question about content Basic JavaScript: Accessing Object Properties with Variables

Tell us what’s happening:

I don’t understand this example and how it works. Specifically, I don’t understand why the “Name” doesn’t have to be “propName”
propPrefix(“Name”); // someProp now holds the value ‘propName’

Sorry, I might be posting this to the wrong place since I did get the code to work.

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"

Your code so far


// 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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36.

Challenge: Accessing Object Properties with Variables

Link to the challenge:

someProp is “propName” because the function propPrefix adds “prop” to the string.

Awesome. Thanks so much. That cleared it right up for me.

I’m glad I could help. Happy coding!