ioSamurai ioSamurai - 1 year ago 80
Javascript Question

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

This way I could have a function that says whatever_way_you_do_this = something. Is this possible? Basically I could tell a function which variable I want to set by giving it a string that holds the name of the variable.


Answer Source


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'];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download