Dynamically access object property using variable

I don’t really understand how to access an object property dynamically using a variable.

Basic JavaScript: Accessing Object Properties with Variables, the second example (see below).

I have been googling around but I didn’t manage to find an additional explanation, which helped. Does anyone have a recommendation? Thanks a lot!

> var someObj = {
> propName: "John"
> };
> function propPrefix(str) {
> var s = "prop";
> return s + str;
> }
> var someProp = propPrefix("Name"); // someProp now holds the value 'propName'
> console.log(someObj[someProp]); // "John"

You have an object called someObj that has only one property called propName
You can access the value directly with someObj["propName"]
You can also access the value using a variable that contain the prop name

var a = "propName";
someObj[a]; // "John"

the variable you use can be built in any way.
In this case you know that the object has all properties with a prefix "prop" so you create a function to add that to any property you want to find, so that if you want the "Name" property, you use the function to change "Name" to "propName"

You need to access property (propName in in someObj) with variable(value that variable holds).
So if your property name is propName as it is in this case, variable needs to hold “propName” value, as a string.
And then you access objects value with brackets notation (someObj[variableValue])

var yourObj = {
  propName: 'John'
}

var variableAccesser = 'propName'

var objPropNameValue = yourObj[variableAccesser]

console.log(objPropNameValue) // logs John




Thanks a lot! You helped a lot!

Thank you very much! It makes sense now.

Currently, the second example given with lesson, ’ Basic JavaScript: Accessing Object Properties with Variables’ shows a user made method/function used to recreate the property name of ‘propName’. Archaeologist03 you simply made a single expression, " var variableAccesser = ‘propName’ " to recreate the property name. Either way what command or set of characters is telling the javaScript language to make the VALUE of the object’s property appear on the screen via console.log?
The comment from the example (entire example is pasted below), // someProp now holds the value ‘propName’, that makes sense to me. Obviously, Archaeologist03 your single expression gets us there faster.

In both examples, yours Archaeologist03 & said lesson, how does the program “know” to make the object’s value appear?

var someProp = propPrefix("Name");   /* someProp now holds the value 'propName'   ........   someProp ONLY has the name of the property  */
console.log(someObj[someProp]);    /*  "John" ...... "John"  why "John"?  Where in this program have we specifically asked for the VALUE of the object which is of course "John"?        console.log(someObj[someProp]);   BUT someProp = propName   .......  what specifically is telling    console.log(someObj[someProp]);     to make the object's value appear?
var someObj = {
  propName: "John"
};
function propPrefix(str) {
  var s = "prop";
  return s + str;
}
var someProp = propPrefix("Name"); // someProp now holds the value 'propName'
console.log(someObj[someProp]); // "John"

Sorry forgot to paste said code…

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