1

below are the global variables that i have defined as....

global1 ="global1 contents";
global2 ="global1 contents";

Now i want to call this in something like this

console.log(global+"1") ; 

how can we call global variable by splitting the last character?

Your Friend
  • 1,119
  • 3
  • 12
  • 27

1 Answers1

1

You can access globals by using bracket notation on the window:

global1 = "global1 contents";
global2 = "global2 contents";

var varName = 'global';
alert(window[varName + '1']);

Note however that it's better practice to put your variables in to their own object to save polluting the window:

var myObj = {
    global1: "global1 contents",
    global2: "global2 contents"
}

var varName = 'global';
alert(myObj[varName + '1']);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339