Access value of JavaScript variable by name?

前端 未结 7 1317
生来不讨喜
生来不讨喜 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:13

    Yes, you can do it like this:

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

提交回复
热议问题