Scope in programming refers to the visibility and accessability of variables. There are two scopes in JavaScript: global and local, and not all varilables are accessible and modifiable throughout the code depending on where the variables are declared.
A variable that is declared outside a function has global scope, and is accessible and can be altered in other loops or functions.
A local-scoped variable is defined inside a function, and can only be accessible within that function. For this reason, a local variable may have the same name as a global variable but have different values.
See the Pen scope by Jae Huh (@Jae-Huh) on CodePen.
In ES6, let
and const
were introduced, and using these, curly braces {...}
can define a block level scope.
See the Pen block level scope by Jae Huh (@Jae-Huh) on CodePen.
Posted on 13 April 2017