August 2016
Intermediate to advanced
635 pages
14h 5m
English
The scope of variables in JavaScript is not natural. In fact, sometimes it's downright counter-intuitive. They say that JavaScript programmers can be judged by how well they understand scope.
First, let's go over the different scope resolutions in JavaScript.
JavaScript uses scope chains to establish the scope of variables. When resolving a variable, it starts at the innermost scope and searches outwards.
Variables, functions, and objects defined at this level are available to any code in the entire program. This is the outermost scope.
var x = 'hi';
function a() {
console.log(x);
}
a(); // 'hi'Each function described has its own local scope. Any function defined within another function has a ...