97
<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>
  const num = 10;
  alert(all_vars['someVar' + 'Name_' + num]);
</script>
4
  • 1
    Your sample shows a global variable. You want to know if you can access it from a local scope? Commented Dec 17, 2009 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 Commented Dec 17, 2009 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? Commented May 29, 2011 at 20:56
  • Most of these answers relate to the title of the question but don't understand what the body text is asking for. Can you clarify, or ideally, edit the question body to match the title? I'm more than happy to do this on your behalf, but I wanted to ask permission first. Commented Nov 24, 2023 at 22:45

6 Answers 6

138

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>
Sign up to request clarification or add additional context in comments.

8 Comments

sorry, I was wrong, i thought that vars are not in window, thanks
I don't care what OP actually wanted. I care about what is in question title and therefore becomes google result in my searches.
What if the variable is object, and I want inner object? for example var a = {name: "ABC"}. Here window["a.name"] will not work. Any workarounds?
@DeepakChaudhary, may be you want something like lodash's _.get? - lodash.com/docs#get
That would be window["a"].name;
|
52

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

1 Comment

This is better than accepted answer. More descriptive about how this feature works in class scopes.
18
<script>
    var someVarName_10 = 20;
    var num = 10;
    alert(eval('someVar' + 'Name_' + num)); //alert 20
</script>

1 Comment

eval worked better for me! but use it with caution
2

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)

Comments

0

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

Comments

-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.

1 Comment

This doesn't answer the question. In the question they have the variable name as a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.