0

I have a function and I want to be able to pass in a string. This string will be used to determine which object variable to access. For example:

//passing in "name" variable as a function. 
//This will determine what object property 
//to use but I don't know how to do it.

var name = "firstName";
function myFunction(name) {
  var person={firstName: "John", lastName: "Doe"};

  console.log(person.name);  
//name is passed in as a string. 
// This is what I want to use to 
//determine which variable of "person" to use.  
}

I have already tried "person.name", "person.[name]", and "person.this.name". Is there a way to do this? Thanks!

Matt123
  • 393
  • 6
  • 18

1 Answers1

0

Try:

person[name] without the fullstop after person.

Russy
  • 36
  • 7