Alright guys. In the last lesson we came across this concept of scope very briefly and I promised that I'd 1
go through it in this lesson. 2
So here we are. 3
Now scope is a really important concept in all kinds of programming and not just Javascript programming 4
but it works a little bit differently from language to language. 5
So in this lesson I'm going to be focusing specifically on Javascript and all of the ways in which Javascript 6
tackles this concept of scope. 7
So what exactly is this concept of scope anyways? 8
So let's say that we've created a function called a 9
and inside the function we create a new variable called x and we set it to equal 2. 10
Now if I tried to console log x inside function a, then of course there's no problems and I would get 11
2 printed in my console. 12
Now creating our variable inside a function is kind of equivalent to having an apple tree inside your 13
backyard 14
right? 15
Your house is surrounded by walls and you've got this apple tree that's inside your garden and you go 16
ahead and you go and pick from your apple tree. 17
Well this is of course possible. 18
So our code in this case when we're using this variable x inside the function where we created the variable 19
x is of course possible. 20
Now let's look at another scenario. 21
Let's say that you create your variable x inside your function a, but you try to access it outside 22
the function. 23
Well this is not going to work because this is equivalent to your neighbor trying to pick the apples 24
from your apple tree in your garden. 25
There's a brick wall that blocks him from accessing your garden. 26
And this is where scope comes in The scope of variable x is local to function a. 27
It's only accessible inside the function a 28
so you can change it, 29
you can use it, you can manipulate it, do anything you will with variable x inside the curly braces of 30
function a 31
but you cannot access it outside those curly braces. 32
Now this also holds true in a third scenario. 33
Say you have two functions a and b. You create the variable x inside function a and you try to use it 34
inside function b. 35
Well that's not going to work either 36
right? 37
Because this is like your neighbor standing in his backyard trying to access the apples in your backyard. 38
That's never going to work. 39
So again in this case, the scope of variable x is limited to inside the curly braces of function a. 40
Now these are called local variables and the scope of local variables are local to where they were declared. 41
So in most cases that's inside the curly braces of a function. 42
Now here's another scenario. What if instead of creating the variable x inside function a, we create 43
it outside the function. 44
So when we write var x, the keyword var states that we're creating this new variable x. 45
And it's created outside the curly braces of any function. 46
Well in this case, not only can you use it inside function a, 47
you can also use it anywhere else inside the code file. 48
And this is equivalent to an apple tree that's growing in the neighborhood. 49
So not only can you the person who lives in a house in the neighborhood pick apples from the apple tree, 50
but your neighbor who lives in a different house can also pick the public apple tree in the neighborhood. 51
So these are known as global variables. 52
Now there's a few peculiarities about Javascript variables in relation to scope. 53
And I just want to quickly talk about it. Now 54
if instead of creating our var inside a function which you can view as say a brick wall that's impenetrable, 55
if we instead created it inside an IF statement or a FOR loop or a WHILE loop or anything other than 56
a function basically then this is kind of seen as a soft wall, like a hedge road. 57
And when you do that, you can in fact access this var x outside the curly braces of the IF block or the 58
FOR loop, or the ELSE block and it's the equivalent of having your neighbor sort of just sneakily going 59
through your hedgerow and stealing your apples. 60
This is completely possible using vars inside Javascript. 61
So this brings us nicely to some other types of variables that we saw previously. 62
So we've seen that you can create a variable by using the var keyword, the let keyword as well as the const 63
keyword. 64
But they have slightly different behavior. Now the biggest difference is between the const and everything 65
else. A constant that gets created cannot be changed once you give it a value. 66
So we've created this const called z and we set it to equal 4. 67
At no point in the future can we change this constant z to anything other than 4. 68
And we're only allowed to use it, 69
so tap into its value elsewhere. 70
Now with var and let we're able to change the its value but they behave slightly differently in relation 71
to scope. 72
So in terms of functions for all three, they are all local. 73
If you create a variable using var, let or const inside a function they all are local variables. 74
They can only be used inside the function. 75
But if you create any three of these types of variables outside the function then they are all global 76
variables and they have global scope. 77
They can be accessed and used anywhere inside the file. 78
Now the third scenario is a little bit weird. 79
If you create a variable inside an IF or an ELSE or a FOR or a WHILE tho anything that has a pair of 80
curly braces essentially a block of code, 81
then the var is a global and you can access this var x anywhere inside your file. 82
But for let and const, they are variables. 83
So that means that if you create a let or a const inside an IF statement then you can only use them 84
inside the IF statement. 85
And this is true for FOR loops, WHILE loops, DO-WHILE loops, any sort of other blocks of code 86
that have a pair of curly braces. So this is something that particular to Javascript because in a lot 87
of other programming languages if you create a variable using the var keyword, it will have local scope 88
inside any sort of code block so FOR loops, IF-ELSE statements etc.. 89
Now what I recommend as good practice is to try and avoid using this keyword var. 90
Now we've been using it up till this point because we weren't yet ready to talk about scope. 91
But now that we understand how it works, it's much safer and more predictable to try and always use let 92
or const whenever you can. 93
There are very very few scenarios where you will need to use the keyword var. 94
And in fact going forward whenever you want to write the word var replace it with let instead. 95
Now this again is a new concept and Javascript has a number of peculiarities that can be difficult for 96
beginners to grasp. 97
So in the resources section of this lesson I've included some reading material and some background reading 98
for you to really get to grips with this concept and also I'd recommend trying it out using let, const 99
and var inside the Chrome developer tools inside the console and trying to use and access and change these 100
variables when they're created inside a function or outside a function or inside an IF statement so 101
that you really understand what's going on yourself. 102
Now in the next lesson we are going to be getting on and styling our to do list to make it look the 103
part instead of just working the part. 104
So for all of that and more, I'll see you on the next lesson.