This question is an exact duplicate:

var per = {}; per.age = 25; var key = 'age'; console.log(per[key]) // будет правильно var person = {}; person.age = 25; var key = age; console.log(person[key]) // будет ошибка 

Reported as a duplicate at Grundy. javascript Dec 26 '18 at 7:42 pm

This question has been marked as a duplicate of an existing one.

    1 answer 1

    Error here:

     var key = age; 

    The variable age not declared.

    • I know that age needs to be wrapped in a string and will be correct, but why? how did YOU write above without wrapping in the string age it is equal to undefined correctly? - xes
    • @xes No It is not equal to undefined . There is none at all. This is the mistake. "Wrap the string" - hmm, there is no "wrapping" here. - Igor
    • in order to avoid errors, in my example, var key = age; age do so 'age' (wrap the string) then alert (person [age]) will print 25 - xes
    • @xes This is understandable - instead of the undeclared variable age will be the string value "age" . - Igor
    • changed the code look pliz - xes