Hello everyone,
I’m new on the forum. I have achieved the responsive web design certification and I’m on my way through the second freecodecamp.org certification on JavaScript algorithms and data structures certification. I am currently stuck on the " Accessing Object Properties with Variables" lesson.
There is this code I don’t quite understand, and this is it:
1 var someObj = {
2 propName: "John"
3 };
4 function propPrefix(str) {
5 var s = "prop";
6 return s + str;
7 }
8 var someProp = propPrefix("Name"); // someProp now holds the value 'propName'
9 console.log(someObj[someProp]); // "John"
Lines 1-3 are clear to me, but I have some questions on the rest of the code:
-
What’s the reason to declare the variable s on line 5 and assign it “prop” ? Couldn’t one just simply write return “prop” + str; on line 6 to make the same effect?
-
This is the real doubt: why on line 8 does propPrefix(“Name”); now holds the value ‘propName’? I mean, the names don’t match.
Thanks in advance to anyone who will be willing to help me out.