1

I have a javascript variable

var varNameValue = "Name";

Now i want to create another variable using the value of varNameValue inside the name of the Other

var x(varNameValue)y = "abc";

So that my new variable name becomes

var xNamey = "abc";

Is there any way to do it. Please help.

3 Answers3

2

Not directly in JavaScript. If you know the content in which it is run, then you can. For example, if this is in a browser, and it is in the global context (which maps to window), then you could do

window["x"+varNameValue+"y"] = "abc";

Similarly if it is on an object

var obj = {};
obj["x"+varNameValue+"y"] = "abc";

But just as a standalone var without any context? Nah.

deitch
  • 14,019
  • 14
  • 68
  • 96
1
var varNameValue = "Name";
eval("var x" + varNameValue + "y = 'abc';")
alert(xNamey);
Vipul Hadiya
  • 882
  • 1
  • 11
  • 22
1

Try

var obj={}

var nameValue="Name";

obj["x"+ nameValue +"y"]= "abc"

Then you can access it using obj.xNamey

KaustubhSV
  • 328
  • 4
  • 15