0

Like, I've a condition like this:

var a="avar", b=3;
var a+b="some value";
// expecting to make, avar3="some value";

Is it possible somehow? Or any alternative way?

Aritra Hazra
  • 426
  • 2
  • 7
  • 17

4 Answers4

5

You can do:

var a="avar", b=3;
window[a+b]="some value";

This will declare a global variable avar3 with value "some value"

omma2289
  • 54,161
  • 8
  • 64
  • 68
2

This:

function ns() { return this; }

var a="avar", b=3;
ns()[a+b] = "some value";

alert(avar3);

will create variable with the name "avar3" in current namespace.

c-smile
  • 26,734
  • 7
  • 59
  • 86
1

No Doesn't work like that.

i dont know if this if what you're trying to do but:

var c=a+b; c="some value";

JohnnyHunter
  • 390
  • 1
  • 3
  • 16
0

you can use object properties for something like this

 var obj = { a: 'avar', b:'3'};
 obj[obj.a+obj.b]='some value';
 console.log(obj.avar3);  //the output is 'some value'

http://jsfiddle.net/fGaFJ/