While working on a JavaScript test, I came across an interesting observation related to the let
keyword. Take a look at this code snippet:
let variable = "I am a variable";
console.log(window.variable);
Surprisingly, when I tried to access the variable
property from the window
object, it was not there. Does this imply that variables declared using let
don't exist in the global scope?
Is it possible to declare variables without having to enclose them within a function or an IIFE (Immediately-Invoked Function Expression)?
I attempted to find answers to these questions online, but no luck so far. The common advice is to use functions like {}
() or work with a distinct global variable containing the code. However, my recent discovery has left me wondering if it can be utilized as an alternative to avoid such approaches.