Since the values in ARRAY and values of mname vars are the same (just the case is different), you could do the following.
var ARRAY = ["A","B","C","D","E"];
var mlength = ARRAY.length;
var month = JAN;
var mnames = ["a", "b", "c", "d", "e"];
//OR
//var mnames = [1, 2, 3, 4, 5];
for (var i = 0; i < mlength; i++) {
var mname = mnames[i];
//instead of mname+i just use mname
}
Your way:
1) Wrap the below code either in head or body with no ready handlers (such as window.onload dom ready etc.) as mname vars will be local and not avilable in the window scope otherwise.
var ARRAY = ["A","B","C","D","E"];
var mlength = ARRAY.length;
var mname0 = 'a';
var mname1 = 'b';
var mname2 = 'c';
var mname3 = 'd';
var mname4 = 'e';
for (var i = 0; i < mlength; i++) {
alert (window['mname'+i]);
}
2) If you are to use the code in one of the ready handlers, you have 2 options. Either define your mname variable with no var keyword or prepend window. like below.
var ARRAY = ["A","B","C","D","E"];
var mlength = ARRAY.length;
mname0 = 'a'; // or window.mname0 = 'a';
mname1 = 'b'; // or window.mname1 = 'b';
mname2 = 'c'; // etc.
mname3 = 'd';
mname4 = 'e';
for (var i = 0; i < mlength; i++) {
alert (window['mname'+i]);
}
Remember, polluitng the global namespace is a bad practice, unless you really need to.
for (var i = 0; i < mlength; i++). see the;andmname+iis not going to work. You may needwindow["mname"+i][i]it should be like this:MARKETS.push(...