Scope

2 August 2018

What 'scope' is, and how does it work in JavaScript?

There are two kinds of scope in JavaScript, global and local. Global scope refers to variables that are defined outside of a function and can be accessed and modified from anywhere. Whereas local variables are defined withtin a function and can only be accessed within the function.

It can analogised to a locked house. The variable inside the locked house (function) has a local scope and cannot be accesed from outside, however a variable outside the house (function) can be accessed from anywhere because it is not bound by the same restrictions as the variable with a local scope.



  • HOME