Dynamic Variables in Javascript
In Javascript under normal circumstances we create all variable by writing “var
In JavaScript there are 2 ways by which you can create dynamic variables:
1) eval Function
2) window object
EVAL Function:
It is said that eval is one of the most powerful function inside javascript. We can execute javascript code, read JSON Object using eval function. But here we will learn how to create dynamic variable using eval function.
var data = "testVariable"; eval("var temp_" + data + "=123;"); alert(temp_testVariable); |
Here i have created an dynamic variable temp_testVariable holding value 123. You can also create arrays using eval function.
var data = "testVariable"; eval("var temp_" + data + "= new Array();"); temp_testVariable[temp_testVariable.length]="Hello World"; alert("Array Length: " + temp_testVariable.length); alert("Array Data: " + temp_testVariable[0]); |
Window Object:
JavaScript Window Object is the highest level JavaScript object which represents the web browser window.
//Creating Normal Variable var data = "testVariable"; window["temp_" + data] = 123; alert(window["temp_" + data]); //Creating Arrays var data = "testVariable"; window["temp_" + data] = new Array(); window["temp_" + data][window["temp_" + data].length]="Hello World"; alert("Array Length: " + window["temp_" + data].length); alert("Array Data: " + window["temp_" + data][0]); |
Related Articles:
- Javascript Optimization Techniques
- Java Plugin detection using JavaScript
- Creating Objects in Javascript
- Check Special Characters in String using JavaScript
- JavaScript Tutorial – Sorting Numeric Array
- Declaring Variables in Javascript
- Object Manipulation in Javascript
- Associative Arrays in JavaScript
- Prototype Function in JavaScript
- Convert XML Document to String in JavaScript
This is awesome!
I was looking for this for 5 hours. Thanks!
thanks – just what i was looking for.
Thanks for this article.
It waz usefull and I learned somting new in JS.
This is not Working in IE !?
Atleast worked for test program in IE, Now time to implement in real time system.
Keeping my fingers crossed
Will let you know if successful …..
Thanks anyway.
Cheers, Sucessfully implemented … Needed to optimze something to support dynamisam..
Your saved my day..
Thanks a lot.
U r simply gr8888
Nicely explained!
Simple and clear article.
Thank you
The eval-function bares security vulnerabilities. So, better decide to use it only if there’s no other way. For object variables, which are hardly accessible or complete inaccessible from the window-object, you can create a object and treat it like an array.
F.e.
var firstVariableName = ‘firstKey’;
var secondVariableName = ‘secondKey’;
var parameters = {};
parameters[firstVariableName] = ‘firstValue’;
parameters[secondVariableName] = ‘secondValue’;
will result in
({firstKey:\firstValue\, secondKey:\secondValue\})
In my case, i had several dynamic parameters to pass over to the data-option on jQuery.ajax();
Hope that helps.
Above code is very useful to me. Thank u.
Thank You! Based on the light you bring I wrote
function $(x){window[x] = document.getElementById(x);
return window[x] }
$(‘txt’).style.height=’100px’; //ok
txt.style.width=’100px’; //ok
next step to build “jAdrian”
) Thank you again
note:
the advantage of eval(“var …”) is that gives the posiblity to create local variables
Thanks a lot !!! this is what i was looking for
Hello All,
In java script we create any type of variable by using “var” keyword. The data type of “var” is decided at run type on the basis of value. In this demonstration I had created several types of variable by using “var” keyword………….. for more details check out this link………………
http://mindstick.com/Articles/e2d167d6-541f-4551-b260-90058aee8587/?Creating%20variable%20in%20java%20script
Thanks !!!!!
Nice one.
Thanks man, you saved me a lot of time.