Key for javascript dictionary is not stored as value but as variable name

Try like this:

var obj = {};
obj[varName] = varValue;

You can’t initialize objects with ‘dynamic’ keys in old Javascript. var obj = { varName : varValue }; is equivalent to var obj = { "varName" : varValue };. This is how Javascript interprets.

However new ECMAScript supports computed property names, and you can do:

var obj = { [varName]: varValue };

Leave a Comment