Review (TL;DR)
Functions are the most common unit of scope in JavaScript. Variables and functions that are declared inside another function are essentially “hidden” from any of the enclosing “scopes”, which is an intentional design principle of good software.
But functions are by no means the only unit of scope. Block-scope refers to the idea that variables and functions can belong to an arbitrary block (generally, any { .. }
pair) of code, rather than only to the enclosing function.
Starting with ES3, the try/catch
structure has block-scope in the catch
clause.
In ES6, the let
keyword (a cousin to the var
keyword) is introduced to allow declarations of variables in any arbitrary block of code. if (..) { let a = 2; }
will declare a variable a
that essentially hijacks the scope of the if
‘s { .. }
block and attaches itself there.
Though some seem to believe so, block scope should not be taken as an outright replacement of var
function scope. Both functionalities co-exist, and developers can and should use both function-scope and block-scope techniques where respectively appropriate to produce better, more readable/maintainable code.
[^note-leastprivilege]: Principle of Least Privilege