Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Possible Duplicate:
How to create object property from variable value in javascript?

How to access property name stored in variable like this?

var obj = {};

obj.foo = 'bar';
var propName = 'foo';

//I want something like this:
console.debug(obj.{propName});

Is there any possibility to do it without using eval()?

share|improve this question

marked as duplicate by katspaugh, Frank van Puffelen, Lex, JaredMcAteer, cdeszaq Dec 11 '12 at 15:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers 2

up vote 1 down vote accepted

You can access the objects properties like:

myObject['property_name']

Try:

var obj = {};
obj.foo = 'bar';
var propName = 'foo';
console.log(obj[propName]);
share|improve this answer

Of cource you can:

​(function(){
    var obj = {};
    obj.foo = 'bar';

    var key = 'foo';

    document.write(obj[key]);
})()​
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.