Variable name for a variable in JavaScript

We use the following syntax to declare a variable with a particular name in JavaScript:


This will assign the value 10 to var1.

But there are situations when you would like to use a name of the variable that would come rather programmatically(dynamically). To facilitate variable(dynamic) name of a variable you may use the following structure of code.

var1="var2";// In place of "var2" there may be random code that must return a string(that will be treated as the variable name you need dynamically).
alert(var2);// alerts 10;

So here var2 may come programmatically which you may not know. In many real time web applications you may come across such a situation when you need this.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s