- Overview
- Transcript
6.3 Scope
Scope is an important and sometimes elusive concept in JavaScript. We need to understand scope, so in this lesson we’ll dig in and focus entirely on what it is.
1.Introduction2 lessons, 07:42
1.1Introduction02:12
1.2Setup05:30
2.Language Fundamentals8 lessons, 1:00:53
2.1Variables06:33
2.2Data Types11:28
2.3Arithmetic, Assignment, and Comparison Operators10:24
2.4Unary, Logical, Comma, and Spread Operators09:02
2.5Operator Precedence03:50
2.6Reserved Words04:17
2.7Strict Mode04:34
2.8Functions10:45
3.Data Structures5 lessons, 22:52
3.1Arrays04:29
3.2Objects04:30
3.3Sets04:57
3.4Maps04:21
3.5Weak Maps and Weak Sets04:35
4.Controlling Program Execution7 lessons, 37:06
4.1Conditionals07:49
4.2Switch Statements04:41
4.3The For Loop06:39
4.4The `for .. in` Loop05:17
4.5The `for .. of` Loop04:02
4.6Iterators05:03
4.7While Loops03:35
5.Using JavaScript13 lessons, 1:44:36
5.1Working With Strings09:32
5.2Template Literals05:46
5.3Working With Numbers06:57
5.4Working With Arrays12:53
5.5Iterating and Transforming Arrays07:33
5.6Working With the Object Type13:55
5.7Object Literal Extensions06:45
5.8Working With Object Instances06:45
5.9Getters and Setters05:00
5.10Custom Objects11:28
5.11The `Math` API04:54
5.12Working With Dates and Times08:10
5.13The `Array` Constructor04:58
6.Functions8 lessons, 56:07
6.1The `this` Object06:15
6.2Working With Functions10:11
6.3Scope07:37
6.4Arrow Functions06:59
6.5Generator Functions08:13
6.6Closures05:00
6.7Prototypes06:26
6.8Default and Rest Parameters05:26
7.Miscellaneous6 lessons, 52:39
7.1Destructuring Assignments08:09
7.2AJAX08:30
7.3Regular Expressions10:51
7.4More About Regular Expressions08:38
7.5Classes06:48
7.6ES Modules09:43
8.Working With the DOM6 lessons, 37:39
8.1Selecting HTML Elements05:02
8.2Manipulating HTML Elements07:40
8.3DOM Traversal05:25
8.4Adding and Removing Elements04:45
8.5Creating Elements and Other Nodes04:39
8.6DOM Events10:08
9.Web APIs4 lessons, 17:41
9.1The Selector API03:03
9.2Geolocation05:29
9.3Web Storage05:24
9.4Web Workers03:45
10.Asynchronous JavaScript5 lessons, 26:23
10.1Promises09:52
10.2Promise Chaining05:11
10.3The async Keyword03:21
10.4The await Keyword04:04
10.5More About async and await03:55
11.Conclusion1 lesson, 00:43
11.1Conclusion00:43
6.3 Scope
Hello folks. In this lesson, we're going to take a look at scope in JavaScript, starting with what it actually is. Scope is actually a relatively simple concept and is a feature of many programming languages. Although JavaScript does have some subtleties, that can be confusing. Scope defines the visibility of variables in different places in our code. In any JavaScript environment, the global scope is the top level scope that contains all other scopes. Typically, the global scope in JavaScript is the window object, which represents the browser that has loaded the page in which the code is running. The global scope is visible to every single script file attached to the page. And this is why we should avoid setting variables in the global scope if it's at all possible. So to inspect the global window object, we can just enter window in the browser console. Any global variables that we create will automatically become properties of the global scope. And we can see that the global variable that we created has automatically been added to the window object. There are many properties of the global objects which are useful. For example, if we want to see the URL that is displayed in the browser's address bar, for example, we can use the location objects, which is a property of the global window objects. So we can see here, the location property contains a location object and that object has a href property which shows the value in the address bar. So I mentioned at the start of the lesson, that the global scope contains all other scopes. So how do we create new scopes? We can do this with either functions or blocks. We we declare a function in JavaScript, it automatically creates its own scope. When we declare variables inside a function, we say that the scope of those variables is local to the function that they're defined within. They are local variables instead of global variables. They're local to the function that they are within. Variables defined inside of functions cannot be accessed or updated outside of those functions. Let's see a quick example of this. We'll need to invoke our function of course. So we can see both of the variables from within the function. So we've got two functions in this example. We've got the immediately invoked function expression which surrounds the whole file. So when this file gets executed, what will happen is this outer wrapping function will create a scope. This scope will belong to the global scope. So inside the scope of the iffy function, the outer containing function, we then define a function called local. And inside this local function, we create a new variable called localvar. When we do the console log, we see that we can log both the myVar, which is part of the IIFE's scope. And the localvar, which is a part of the local scope. So the local scope is actually within the IIFE scope, the IIFE scope itself is within the global scope. Inside of the local function then, we can see any variables that are defined within the function itself. So any variables that are local to the function, we can see all of those inside that function. But we can also see any variables which are in any scope, which the local function is also within. So the local function is within the IIFE function scope, where myVar is defined. And so we can see that both inside the local function and outside of the local function. However, we can't see the local variable outside of the local function In this case we get an error to say that localvar is not defined. Now we know that localvar is defined, but it's defined in the local functions scope. So, we can reach out of scopes like inside the local function, we can reach out to the containing scope to see the myVar variable. But we can't reach into a scope, we can reach into the local function scope in order to see the local variable from outside of the local function, that doesn't work. So all of these different scopes that get created are known as scope chains. So inside of the local function, we are at the end of a scope chain which included the local scope, the IIFE scope and the global scope. When we are in the IIFE scope, the scope chain just consists of the IIFE scope and the global scope. As well as function scope, JavaScript now also has block scope. Block scope enables the same behavior as function scope, but for blocks of code instead of functions. A block of code, remember, is any statements within curly braces. So we've created a block using a for in loop here. Let's run the code in a browser and see what happens. So you can see that we do see prop one in the console, but then we see an error to say that prop is not defined. So while we are inside the block of the for loop, we can access the prop variable. But because the prop variable was declared using the lets keyword, that makes the variable block scope, so it can only be seen within the block of the for loop. And when we try to access prop outside of the for loop, we see the error. If we change let to be var instead, we then don't see the error anymore, because the variable is no longer block scope. So actually now prop exists in the IIFE scope, because is its containing scope. So in this lesson we looked at scope in JavaScript and we saw that it can be defined as the area in which variables can be accessed in different parts of our code. We saw that variables defined in the global scope are visible everywhere, even in different script files running on the same page as our own. We also saw that functions create their own scope and that variables defined inside a function are only visible to other code within the same function. We also saw that blocks can create their own scope, as long as we use either the let or const keywords in order to declare variables. If we use the var keyword to declare a variable inside a block, that variable will leak out of the block and be visible in code outside of the block. In the next lesson, we’re going to move on to look at arrow functions. Thanks for watching.