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);
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103