Accessing Object Properties with Variables example explaination

I was wondering if somebody could explain to me what the mid section of the example code does. I can’t see how the var s section has anything to do with the actual coding but if it is removed, the code doesn’t run on Repl.it. I can pass the test, I’m just very curious.

var someObj = {
  propName: "John"
};
//what does this section do?
function propPrefix(str) {
  var s = "prop";
  return s + str;
}
var someProp = propPrefix("Name");
//someProp now holds value "propName"
console.log(someObj[someProp]);
//john
1 Like

I’m sorry, I just still don’t understand what the var s does

Why is it that if the “prop” in var s = “prop” is changed to anything else, it won’t work?
It is the only instance of the “prop” string and is not a keyword. Can you please explain further or point me to some resources? I am very confused haha. Thanks :smile:

Why would anyone want to access the property of an object like that? I’m sure there must be a real life scenario where this would be useful, but I don’t see it.

1 Like

Just right now, after this example I managed to understand the explanation. Thanks!

1 Like