How to get variable name?

If

var testint = 1;
var teststr = "val";
var val1 = 5;
var val2 = 8;
var val3 = 4;

then

window[teststr + testint.toString()]

will output 5 (the value of variable val1).

Similarly, if

var testint = 2;
var teststr = "val";
var val1 = 5;
var val2 = 8;
var val3 = 4;

the outputed value will be 8 (value of variable val2).

Browse More Popular Posts

Leave a Comment