Get global variable dynamically by name string in JavaScript

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>

Leave a Comment