The Scope Chain
the scope chain is the mechanism used by a javascript runtime that references a chain of parent scopes to resolve an identifier
when an identifier is referenced but not located in the "local scope", the scope chain traverses up to a parent scope until the identifier is found

var peanuts = 'peanuts';
function allergies() {
return peanuts;
}
allergies();Scope Chain Traversal

Nested Functions
var newYorkBoroughs = ['Brooklyn', 'The Bronx', 'Manhattan', 'Queens', 'Staten Island'];
function manhattan() {
var manhattanLS = 'manhattan';
function soHo() {
var soHoLS = 'soHo';
function chelsea() {
var chelseaLS = 'chelsea';
console.log(manhattan, soHoLS, chelseaLS, newYorkBoroughs);
}
chelsea();
}
soHo();
}
manhattan();
Scope Chain Traversal Example

Nested Functions
var newYorkBoroughs = ['Brooklyn', 'The Bronx', 'Manhattan', 'Queens', 'Staten Island'];
function manhattan() {
var manhattanLS = 'manhattan';
function soHo() {
var soHoLS = 'soHo';
}
soHo();
}
function chelsea() {
var chelseaLS = 'chelsea';
console.log(chelseaLS, newYorkBoroughs);
}
chelsea();
manhattan();
Nested Functions

Reference Error

var newYorkBoroughs = ['Brooklyn', 'The Bronx', 'Manhattan', 'Queens', 'Staten Island'];
function manhattan() {
var manhattanLS = 'manhattan';
return brooklyn;
}
manhattan();The Scope Chain
By Scott D'Alessandro
The Scope Chain
- 734