Why did it return Undefined

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

var someProp1 = propPrefix("name");
console.log(someObj[someProp1]);

Hi @MohammedSalman !

May I ask you why you are creating a function in the first place?
It just seems like a lot of extra work when you can just do
someObj.propName

But to answer your question when you use this function call

and then you try to do someObj[someProp1] there is no property of propname.
That explains why this code returns John

and this code returns undefined

If you add a console.log(someProp1)
then you will see it returns “propname”

and there is no propname here

Hope that makes sense!

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