0
var q2312="{'hello world'}";var autogenname="q2312";

I auto generate a variable called "autogenname" from razor pages where the content refers to the defined variable. e.g. "q2312" refers to the defined variable name q2312. How do i access the var q2312?

Gino
  • 39
  • 1
  • 8
  • Does this answer your question? [Get global variable dynamically by name string in JavaScript](https://stackoverflow.com/questions/1920867/get-global-variable-dynamically-by-name-string-in-javascript) – A_A Oct 03 '20 at 06:13

1 Answers1

1

You can access global variables (defined with var) via window

var q2312="{'hello world'}";
const autogenname="q2312";

console.log(window[autogenname])

I don't know your exact use case, but in general I wouldn't recommend doing it over the global space. Instead you could store the variables in an object and then access it via the keys:

const myData = {
 q2312: 'Hello World'
}

const autoGenName="q2312";

console.log(myData[autoGenName])
A_A
  • 1,832
  • 2
  • 11
  • 17