+ 2
How do I declare a variable with the name that is the value of another variable?
Suppose i have a var $name = prompt("Please enter your name");. Now, I want a variable, whose name will be the value of the $name. What I mean is suppose a user wrote Bob. then $name will have the value "Bob". I want a variable named Bob which will have properties like name , age , gender etc. I mean I want a object named Bob, but I don't know that the object's name is Bob before.... Something like AUTOMATION. Can anyone please help me with this?
2 Answers
+ 3
Maybe you can use eval(). But people say it is not safe to use eval()
let myName = "Bob";
eval( `${myName} = {'age' : 3, 'breed' : 'Husky'}` );
alert( JSON.stringify( Bob) );
+ 1
Thanks