Why is the Object Property accessed like that?

So I’ve completed the Basic JavaScript: Accessing Object Properties with VariablesPassed , I understand how the function propPrefix is used to concatenate the two strings “prop” and “name”, but why is it like that? That’s what I’m curious about.

To demonstrate passing variables to an object lookup. It’s a contrived example, but it’s so that you get practice of it.

1 Like

Well I see now, but generally used are the “obj.name” and "obj[“name”] methods right?

Just whatever is necessary in the code for the program you’re writing. Using objects directly happens a lot in JS programs (and indirectly, everything is an object, so it’s constant in every JS program). So dynamically accessing properties is very common.

Ok Dan, Thanks a lot!

1 Like