Language - Scope of an identifier (Variable, Package, Lexical)


Scope determines where and how identifiers (variable, function, class) are stored.

See also, same as : Language - Namespace (Scoping rule for name)

A scope can be nested inside another scope. If one scope is nested inside another, code inside the innermost scope can access variables from either scope.

Lexical scope rules say that code in one scope can access variables of either that scope or any scope outside of it.


  • Lexical Scope - The most common
  • Dynamic Scope (such as Bash scripting, some modes in Perl, etc.)


As the scope is generally dependent of the parse tree, scope are nested.


The scope of a variable is the context in which it's visible to the program.

Many languages use block-level scope, in which variables exist only within this current “block” of code but some other use the function-level as javascript

Scope is basically a collection of variables as well as the rules for how those variables are accessed by name.

A variable name has to be unique within the same scope. But the same variable name a could appear in different scopes.


  • global variables: are available everywhere
  • member variables: are only available to members of a certain class
  • instance variables: are only available to particular instances of a class

Powered by ComboStrap