Accessing Object Properties with Variables, why would john be printed

var someObj = {
  propName: "John"
};
function propPrefix(str) {
  var s = "prop";
  return s + str;
}
var someProp = propPrefix("Name");
console.log(someObj[someProp]);

now why would John also be printed

Hi @Domini !

Welcome to the forum!

When the function is called here

it will return “propName”.

We are then assigning “propName” to the variable someProp.
When you do this console.log

what you are really saying is this.

someObj["propName"] // "John"

Hope that makes sense!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.