As we explored in Chapter 2, scope consists of a series of “bubbles” that each act as a container or bucket, in which identifiers (variables, functions) are declared. These bubbles nest neatly inside each other, and this nesting is defined at author time.
But first, let’s explore function scope and its implications.
Consider this code:
// some code
// more code
In this snippet, the scope bubble for
foo(..) includes identifiers
bar. It doesn’t matter where in the scope a declaration appears, the variable or function belongs to the containing scope bubble, regardless. We’ll explore how exactly that works in the next chapter.
bar(..) has its own scope bubble. So does the global scope, which has just one identifier attached to it:
bar all belong to the scope bubble of
foo(..), they are not accessible outside of
foo(..). That is, the following code would all result in
ReferenceError errors, as the identifiers are not available to the global scope: ...