How to make a variable addressable by name as string in JavaScript?

Use a Javascript object literal:

var obj = {
    a: 1,
    b: 2,
    c: 'hello'
};

You can then traverse it like this:

for (var key in obj){
    console.log(key, obj[key]);
}

And access properties on the object like this:

console.log(obj.a, obj.c);

Leave a Comment