1
var str="firstname";
var obj={};
obj.str="john";

I want to create property firstname but i want to create it by the variable name like obj.str not like obj.firstname here problem is obj.str create property str not firstname. i want to create property like this because it will later help me to create property by joining two string .

1 Answers1

1

Try like this with [] notation. See MDN when to use dot(.) or bracket([]) notation for javascript.

var str = "firstname";
var obj = {};
obj[str] = "john";
console.log(obj);
Always Sunny
  • 32,751
  • 7
  • 52
  • 86