DEV Community

Jasmin Song
Jasmin Song

Posted on • Updated on

Scope

Scope is what we can access and where we can access it. It is the concept of where something is available.

Through the scope chain, a function has access to all variables and functions declared in its outer scope.

Image description

3 levels of scope

Global

  • Accessible in every scope
  • Outermost scope

Functional

  • Accessible within a function
  • If you have 2 functions, they have their own unique scopes. You can't access the same items from each other's boxes.
  • Specific to a single function
  • Can't be accessed by anything in outer scopes
  • Every function creates its own scope, and any variables or functions you declare inside of the function will not be available outside of it.

Block

  • Accessible within a block
  • Specific blocks
  • Can't be accessed by anything in outer scopes
  • Variables declared with var are not block scoped. Variables declared with const and let are block scoped.

Lexical Scope

  • Can access variables in outer scope
  • Can't access variable in nested scope

Image description

Cannot access variables in parent scope.

Image description

Top comments (0)