This makes no sense to me

I am doing the Basic JavaScript course right now, and this module is called “Accessing Object Properties with Variables.”
It shows two ways to do this, and the first one made sense to me. You have a var which seems to have a string of values being assigned to other strings.
Then you assign one of those values to something like myVar, then you access the value of myVar using bracket notation. Something like…
var myExample: = var[myVar];
console.log(myExample);
(I don’t know if this explanation makes sense, but maybe it’s helping me to understand it better.)

but anyway, all of this to say that the SECOND way to accomplish this task makes absolutely no sense to me. I don’t need it to pass the test on this exercise, but I feel like maybe I would need to understand this operation in the future?! If anyone has time to break it down for me, I would appreciate it. The function is confusing me. What is str? has that been defined elsewhere?

Hi @micha.ella !

In the future, it would be best to write your code into the forum instead of posting screenshots.


str is a function parameter for propPrefix.
str is a placeholder for the real value when the function gets called like this propPrefix("Name")

What the function is doing is assigning "prop" to a variable called s.
Then it is return s concatenated with str

When we call the function propPrefix("Name")
"prop" is concatenated with "Name"

The function returns "propName"

When you do this console.log(someObj[someProp]
You are can rewrite it like this

console.log(someObj["propName"]

And that result will give you "John"

Hope all of that makes sense!

1 Like

Sorry I didn’t know about the screenshot rule. This operation still doesn’t make sense to me really, but maybe I just need to rest and come back to it tomorrow. I appreciate your explanation.

I think once you work with functions more then this example will make more sense.

Don’t worry to much about it now.

1 Like

str is just a placeholder. we call it parameter on the function and argument when we call it. Let’s say placeholder this time.

For example you need a function that sums 2 to any number you pass to that function. we write a function like this:

function sum(a) {
  return a + 2;
}

and we call the function with the number we want to sum:

sum(5);

Our function will sum 5 + 2 and return the value “7” (5 + 2).

the number 5 will replace the placeholder(parameter) in this case “a”:

function sum(5) {
  return 5 + 2
}

This “a” is not declared like a variable is just a parameter, a placeholder to be replaced wi anything you pass to the function, everytime that there is an “a” in your function will be replaced with the value you pass when you call the function:

function myFunction (str) {
    console.log("This is my " + str + " and here is my " + str " again.");
}

myFunction("STRING");

This will output on the console: “This is my STRING and here is my STRING again.”

I hope you understand now. To remember the real names (the word “placeholder” is just for explain it to you):

function sum(a) {
  //here "a" is called a parameter
}

sum(5); //here we call it "argument"
1 Like

Thank you for breaking down functions and placeholders. That is helpful.

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