-1

I want to make a variable dynamically. Example-

var a ="pres"+b;

where b is a variable, and then use a as a different variable.

SBI
  • 2,322
  • 1
  • 17
  • 17
AAA
  • 142
  • 7

3 Answers3

1

You'll be in a much confortable solution using an object to store values, and the bracket notation :

var store = {};

var theEnd = 'Something';

store['b'+ theEnd] = 10 ;

store['c'+ theEnd] = 20 ;

You can easily iterate in existing keys and values with :

 for (var key in store) {
     var value = store[key];
     console.log(' store has key:' + key + '  having value ' + value);
 }

 // output :  
 // store has key bSomething having value 10
 // store has key cSomething having value 20
GameAlchemist
  • 18,995
  • 7
  • 36
  • 59
0

u have to use eval() to do this... but dont be eval! this is not a good style!

Cracker0dks
  • 2,422
  • 1
  • 24
  • 39
0

Your question is a no logical; Is normal that a and b are variables|||

You have to use new String("string"); and in your case

 var a = new String("pres")+b ; 

but you can use simplier var a ="pres"+b;

Mirko Cianfarani
  • 2,023
  • 1
  • 23
  • 39