94
<script>
//in one script
var someVarName_10 = 20;
</script>

I want to get access to this variable from another script by name of variable. With window object its simple, is it possible with local variable?

I mean access this var by code like this:

<script>
  alert(all_vars['someVar' + 'Name' + num]);
</script>
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
Igor Golodnitsky
  • 4,456
  • 7
  • 44
  • 67
  • 1
    Your sample shows a global variable. You want to know if you can access it from a local scope? – Crescent Fresh Dec 17 '09 at 10:46
  • It really depends on what you mean by 'local scope'.. Javascript functions are scoped only to functions, not blocks or even files – K Prime Dec 17 '09 at 10:50
  • possible duplicate of [Is there a way to access a javascript variable using a string that contains the name of the variable?](http://stackoverflow.com/questions/1441532/is-there-a-way-to-access-a-javascript-variable-using-a-string-that-contains-the-n) – Gilles 'SO- stop being evil' May 29 '11 at 20:56

6 Answers6

136

Do you want to do something like this?

<script>
//in one script
var someVarName_10 = 20;

alert(window["someVarName_10"]); //alert 20

</script>

Update: because OP edited the question.

<script>
  num=10;
  alert(window['someVar' + 'Name_' + num]); //alert 20
</script>
YOU
  • 120,166
  • 34
  • 186
  • 219
50

I've noticed that everyone is advising global var creation this will lead to variables leak to global namespace. When you dynamically creating classnames or just variables it's easy to keep em local:

this['className'] = 123;

or

this['varName'] = 123;

Name-spacing would look like this:

vars = {};
vars['varName'] = 123;
vars.varName // 123
yckart
  • 32,460
  • 9
  • 122
  • 129
Andrew Shatnyy
  • 1,528
  • 14
  • 19
  • 2
    This is better than accepted answer. More descriptive about how this feature works in class scopes. – Sellorio Jun 22 '15 at 01:54
17
<script>
    var someVarName_10 = 20;
    var num = 10;
    alert(eval('someVar' + 'Name_' + num)); //alert 20
</script>
Matteo Baroni
  • 179
  • 1
  • 3
1

well, for debugging purpose only, you could do something like this. I use it during the development of classes, where some variables must remain private (var). this work even in local variable (and global of curse)

function MYCLASS(){
    var a=1, b=2, c=3;
    this.public = "variable";
    this.debug = function(sVar){
        return eval(sVar);
    }
}

var myThing = new MYCLASS();
myThing.debug('a') //return 1
myThing.debug('b') //return 2
myThing.debug('c') //return 3
Nereo Costacurta
  • 7,693
  • 4
  • 21
  • 27
1

This is not my own answer, its Arthur Araújo's answer, but I think its relevent here.


Eval alternative:

exp = '1 + 1'
x = Function('return ' + exp)()
console.log(x)
Elron
  • 1,235
  • 1
  • 13
  • 26
-9

If this is what you said:

<script type="text/javascript">
var hello = 'test';
</script>
<script type="text/javascript">
  alert (hello);
</script>

It works because script are finally available to the document and you can access their vars.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578