Search code examples
javascriptwindow

javascript window.var_name vs window['var_name']


Which of these variants is more accepted by most browsers or more recommended to use ?

window.var_name;
//or
window['var_name'];

Solution

  • This doesn't make any difference - both of those ways are the ways of getting the value of the object by name.The second one is in case you want to get the value by a name which is stored in the variable (or a result of an expression).

    In your case, the object is window, so you could either get that value with just var_name.

    var var_name = "test";
    console.log(var_name);
    console.log(window.var_name);
    console.log(window['var_name']);