Skip to main content

Archive

Show more

How to dynamically access object property using variable in Javascript

how-to-dynamically-access-object-property-using-variable-in-javascript


Question: How to dynamically access object property using variable in Javascript

Answer:

var myObj = {
  name: 'RustcodeWeb'
};

var newVariableName = "name";

console.log(myObj[newVariableName]);
console.log(myObj.name);

Output:

"RustcodeWeb"
"RustcodeWeb"


We try to provide you the best content, if there is any mistake in this article or there is any mistake in code, then let us know.

Comments