1
00:00:00,210 --> 00:00:05,820
This lesson we're going to be looking at function scope, so go global scope of variables and values

2
00:00:05,820 --> 00:00:08,790
and local scope of variables and values.

3
00:00:09,100 --> 00:00:14,410
So they're really the difference between the scope is where the value of the variable is declared.

4
00:00:14,610 --> 00:00:21,240
So you see in this case that we've got a variable rate and we've called it Tarlow and then we've got

5
00:00:21,480 --> 00:00:28,350
variable B what this what this one is only living within the function so we can console logout E plus

6
00:00:28,350 --> 00:00:30,020
B while we're in the function.

7
00:00:30,030 --> 00:00:32,970
So we invoke the function and everything is great.

8
00:00:33,210 --> 00:00:33,660
We'll see.

9
00:00:33,660 --> 00:00:34,440
Hello World.

10
00:00:34,590 --> 00:00:40,440
But then when we do console log B and this is trying to get a variable that lives within the function

11
00:00:40,590 --> 00:00:45,470
and we're not able to access it because we see B is not defined.

12
00:00:45,780 --> 00:00:51,800
So the thing that is happening here is this is having to do with the scope of the variables.

13
00:00:52,200 --> 00:00:58,440
So the way that variables work and within functions, you can access the value only within the block

14
00:00:58,440 --> 00:01:00,850
of code or its parent.

15
00:01:01,140 --> 00:01:08,850
So if this particular variable is not declared and we've got our root scope here, so the AI is on the

16
00:01:08,850 --> 00:01:12,270
root and we're trying to look for B on the root as well.

17
00:01:12,540 --> 00:01:18,210
And the reason we're not able to find B is that it's not on the root and it's not on the parent and

18
00:01:18,210 --> 00:01:19,710
the root doesn't have a parent.

19
00:01:19,950 --> 00:01:27,720
So that's why we're not able to find any value for B, whereas within the function we look for B and

20
00:01:27,720 --> 00:01:36,960
then we've got Consolo A plus B, so JavaScript first looks for this block of code within the function

21
00:01:36,960 --> 00:01:39,240
and it sees no end there.

22
00:01:39,390 --> 00:01:45,600
So it automatically reverts back to the parent and it looks for that same variable being defined within

23
00:01:45,600 --> 00:01:46,140
the parent.

24
00:01:46,440 --> 00:01:51,510
And if we had another parent and another parent, it would continuously look into the parents until

25
00:01:51,510 --> 00:01:52,980
it actually found that variable.

26
00:01:52,980 --> 00:01:57,290
And if the variable was not there, then it would still show us that undefined.

27
00:01:57,780 --> 00:01:59,140
So that's how the scope works.

28
00:01:59,160 --> 00:02:02,670
And I'll give you a quick example of that and the way that that can work.

29
00:02:02,680 --> 00:02:10,920
So if we set up a variable and we've got a value of five, four, eight, and then within test, we

30
00:02:10,920 --> 00:02:19,380
can console log out a and then we invoke test, you can see that the value of A gets placed within the

31
00:02:19,380 --> 00:02:19,860
function.

32
00:02:20,250 --> 00:02:27,810
Now what happens if we take a and if we redeclared it only within the function itself.

33
00:02:28,260 --> 00:02:31,420
And remember, we're still outputting a within the console.

34
00:02:31,650 --> 00:02:34,250
So this actually leads to two different A's.

35
00:02:34,560 --> 00:02:41,310
And as mentioned earlier, the way that JavaScript will work is it will look for a within the current

36
00:02:41,310 --> 00:02:41,850
block.

37
00:02:42,060 --> 00:02:45,520
And if it finds it, then it will use this value of a.

38
00:02:45,810 --> 00:02:54,300
So even if we try to get a value of a before we declare it, we see that it's not declared because it

39
00:02:54,300 --> 00:02:55,590
doesn't go to the parent.

40
00:02:55,740 --> 00:03:04,260
It knows that we've got a value of a that is within this block, but we know that it isn't declared

41
00:03:04,440 --> 00:03:04,980
first.

42
00:03:05,160 --> 00:03:07,800
So we need to declare it first before we try to use it.

43
00:03:07,980 --> 00:03:09,770
And that's why we're throwing that error.

44
00:03:10,080 --> 00:03:13,560
So if we comment that out, then what happens?

45
00:03:13,560 --> 00:03:20,490
JavaScript goes through, invokes the function and sees that there's no E available within this block,

46
00:03:20,490 --> 00:03:24,360
a code that's being declared and it will revert back to the parent.

47
00:03:24,630 --> 00:03:26,220
So go ahead and try this out.

48
00:03:26,430 --> 00:03:32,400
And it does take some getting used to so you can try to declare various variables, declare some variables

49
00:03:32,400 --> 00:03:35,400
within a function and then use them within your code.
