-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.

0

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

what about window variable? eg window[a]
why not using an object? if u have to convert it to string and back use JSON
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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.