Access value of JavaScript variable by name?

前端 未结 7 1309
生来不讨喜
生来不讨喜 2020-11-27 07:24

Hello it is possible to access the value of a JavaScript variable by name? Example:

var MyVariable = \"Value of variable\";


function readValue(name) {
             


        
7条回答
  •  爱一瞬间的悲伤
    2020-11-27 08:14

    Global variables are defined on the window object, so you can use:

    var MyVariable = "Value of variable";
    alert(window["MyVariable"]);
    

提交回复
热议问题