Is there a way to access a javascript variable using a string that contains the name of the variable?

Given:

var x = {
    myproperty: 'my value'
};

You can access the value by:

var value = x['myproperty'];

If you’re looking for a global variable, then you would check its container (window);

var value = window['x']['myproperty'];

Leave a Comment