Accessing Object Properties with VariablesPassed

This is the code from one of the Basic Javascript courses;

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

What I understood from this is that we returned the variable ‘s’ + str in the propPrefix function. And because we entered the string “Name” as the vlaue for (str) and the value of variable ‘s’ is set to “prop”, therefore it returned “propName”. Thus the console will show “John” as declared in the object’s property.

What I dont understand is, should it not return “prop”, “Name” (as two different strings or an array).

Kindly help. Thank you!

This is string concatenation. String concatenation creates one string out of two, so you get one string stored into sompProp.

1 Like

Thanks you for your response. I get it now.

I also found your post, it is really helpful.

1 Like

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