Search code examples
javascriptglobal-variableslocal-variables

How to access the global window variable if there's a local window variable?


Just out of curiosity, how would you access the global window object inside a function with this signature:

function bla(window){
   // How to access the global 'window' object here? Because 'window' is now local.
}

Solution

  • You could use globalThis

    a = 1;
    
    function x(window) {
      console.log(window.a)
      console.log(globalThis.a)
    }
    
    x({ a: 2 })