Scope is the region of code where a variable is accessible. The outermost region is the global scope, available everywhere in a script. Variables declared with var are function-scoped, meaning they are visible throughout the function in which they are declared regardless of block boundaries, while let and const are block-scoped, limited to the block such as a {}, if, or for body in which they appear. Hoisting describes JavaScript's behavior of moving declarations to the top of their scope before code runs. var declarations are hoisted and initialized to undefined, so a variable can be referenced before its declaration line without throwing. let and const are also hoisted but live in the Temporal Dead Zone until their declaration line, which throws if you try to read them earlier.
A closure is a function together with the variables of its outer scope that the function continues to reference, even after that outer function has finished executing. Closures are the basis for data privacy and the module pattern, because an inner function can capture local state that no one else can see. Closures only work because of lexical scope, which is the rule that a function's available variables are determined by where it was written in the source code, not by where it is eventually called. Confusing lexical scope with dynamic scope—for example, assuming a function sees the variables of wherever it is called rather than wherever it is defined—is a common pitfall, especially for developers coming from class-based languages.
Closures and scope rules combine to enable several useful patterns. An IIFE (Immediately Invoked Function Expression), written (function() { ... })();, runs once on definition and creates a private scope that prevents variables from leaking into the global namespace. JavaScript modules extend this idea across files: a module can export values, and another file can import them, organizing code and avoiding global namespace pollution. Each of these techniques—closures, IIFEs, modules—begins with the same principle that scope is determined by the lexical structure of the source.