1
00:00:00,750 --> 00:00:06,610
All right guys so I think now that you know functions are in you know loops and if statements I think

2
00:00:06,610 --> 00:00:09,020
it that now it's time to talk about scope.

3
00:00:09,210 --> 00:00:16,590
Okay I wanted to talk about scope because of LET and Konst versus Vire earlier but for those of you

4
00:00:16,590 --> 00:00:21,420
that didn't know what a function was or how it was set up or if statements I didn't want to go over

5
00:00:21,420 --> 00:00:21,840
it yet.

6
00:00:21,840 --> 00:00:25,300
So now that we've already done that let's talk about scope.

7
00:00:25,300 --> 00:00:33,100
So what I'm going to do is create a couple of variables and just tell you that this is the global scope.

8
00:00:33,150 --> 00:00:36,800
OK we're not inside of anything any functions or anything like that.

9
00:00:36,800 --> 00:00:38,360
This is the global scope.

10
00:00:38,460 --> 00:00:44,300
I'm going to set three variables using all three methods so var A equals.

11
00:00:44,850 --> 00:00:48,900
And we'll just say well it's just two numbers.

12
00:00:48,900 --> 00:00:50,240
We'll just do one.

13
00:00:50,520 --> 00:01:00,980
So var equals 1 and then we'll do a let B equals two and then we'll do a contest C equals three.

14
00:01:01,370 --> 00:01:15,960
OK and then down here will Konsole dot log and we'll say global scope A B C and save and you can see

15
00:01:15,960 --> 00:01:17,870
global scope 1 2 3.

16
00:01:17,870 --> 00:01:19,840
So this is var A.

17
00:01:19,860 --> 00:01:21,070
This is.

18
00:01:21,150 --> 00:01:21,780
Let be.

19
00:01:21,780 --> 00:01:26,670
This is Conte's see OK so everything's fine that's what you would expect.

20
00:01:26,670 --> 00:01:28,320
Now let's do a function.

21
00:01:28,400 --> 00:01:31,250
So say function test.

22
00:01:31,680 --> 00:01:40,640
And let's again define three variables except they're going to change the value to 5 not 5 for that.

23
00:01:40,890 --> 00:01:52,040
So 4 5 and 6 and then we're going to just console log in here and this time let's say function scope

24
00:01:52,610 --> 00:01:54,610
and let's see what we get.

25
00:01:54,620 --> 00:01:55,900
Hope we get to run the function

26
00:01:58,750 --> 00:01:59,180
OK.

27
00:01:59,190 --> 00:02:06,030
So function scope we get four five and six global scope we get 1 2 and 3.

28
00:02:06,030 --> 00:02:12,820
So basically these are these are separate variables than these are because they're in a different scope.

29
00:02:13,020 --> 00:02:13,470
OK.

30
00:02:13,470 --> 00:02:14,940
See how that works.

31
00:02:14,940 --> 00:02:19,080
Var is of a function scope declaration.

32
00:02:19,080 --> 00:02:26,090
Meaning that you can have a different a different variable of the same name inside of a function.

33
00:02:26,250 --> 00:02:27,420
Then on the outside.

34
00:02:27,420 --> 00:02:30,100
So this is going to affect this.

35
00:02:30,330 --> 00:02:34,600
Now we also have something called block level scope.

36
00:02:34,670 --> 00:02:38,240
But what I mean by block is an if statement a loop.

37
00:02:38,250 --> 00:02:47,070
Anything that is wrapped in curly braces so let's put an if statement here and it doesn't matter what

38
00:02:47,070 --> 00:02:48,820
we put in here let's just say true.

39
00:02:48,820 --> 00:02:50,730
So it definitely runs.

40
00:02:50,880 --> 00:02:56,440
And then we're going to put a comment here and this is a block scope.

41
00:02:56,820 --> 00:02:57,490
OK.

42
00:02:57,550 --> 00:03:07,270
And we'll take these variables and we're going to set them to 4 5 and 6 and then let's do a console

43
00:03:07,290 --> 00:03:18,090
log within the if and we'll say or let's say if scope it is a Blasko but we'll just say f and save and

44
00:03:18,090 --> 00:03:19,010
let's see what happens.

45
00:03:19,010 --> 00:03:24,830
So this is the scope we get four five and six which is fine.

46
00:03:24,870 --> 00:03:27,830
Now look at the global scope global scope.

47
00:03:27,830 --> 00:03:30,450
We have four to three.

48
00:03:30,480 --> 00:03:37,050
So what's happened is we defined a in the global scope and then within the if scope or within the block

49
00:03:37,050 --> 00:03:45,300
scope of the if statement that a variable was changed because when we Eckl or when we Konsole log it

50
00:03:45,300 --> 00:03:52,810
out back in the global scope it's changed to 4 where it was assigned inside of the half.

51
00:03:53,060 --> 00:03:53,540
OK.

52
00:03:53,550 --> 00:03:56,790
Notice leten Konst is still 2 and 3.

53
00:03:56,970 --> 00:04:01,950
Even though inside the block scope we set them to 5 and 6 they're still in there still two and three

54
00:04:01,950 --> 00:04:05,810
in the global scope because that's what we initially declared them as.

55
00:04:06,000 --> 00:04:12,540
So the way that Latin cons to work is how most programming languages work.

56
00:04:12,660 --> 00:04:19,500
Vire what is kind of weird and then it's one of the things that a lot of people didn't like about javascript

57
00:04:20,150 --> 00:04:27,300
because of security Caesarians causes security risks and it can just cause confusion if you have some

58
00:04:27,300 --> 00:04:34,650
variable in the global scope and then inside a block some program has the same variable name.

59
00:04:34,650 --> 00:04:36,500
Things can get messed up.

60
00:04:36,540 --> 00:04:39,260
So let's now try this with a loop.

61
00:04:39,640 --> 00:04:43,970
OK so we're going to do a for loop.

62
00:04:44,390 --> 00:04:45,150
All right.

63
00:04:45,200 --> 00:04:48,290
And what I'm going to do is I'm going to first use let.

64
00:04:48,380 --> 00:05:03,110
OK so I'm going to say Let let's do let a he calls 0 and then we'll say as long as a is less than 10

65
00:05:03,800 --> 00:05:11,580
and then we'll do a plus plus and then console dog log.

66
00:05:11,640 --> 00:05:16,790
They actually put a template string in here and it's a loop

67
00:05:19,810 --> 00:05:22,930
say loop and save.

68
00:05:22,940 --> 00:05:28,250
All right so each value for a in the loop is going to be 0 1 2 all the way up to 9.

69
00:05:28,430 --> 00:05:36,970
But in the global scope a success shouldn't that be since I did let be here.

70
00:05:37,070 --> 00:05:42,290
Well no actually that doesn't matter doesn't matter because we said we set a to one here whether it's

71
00:05:42,290 --> 00:05:43,610
fire or let.

72
00:05:43,610 --> 00:05:46,050
What matters is that we're using LET here.

73
00:05:46,280 --> 00:05:48,160
So it's stayed as one.

74
00:05:48,440 --> 00:05:49,210
OK.

75
00:05:49,250 --> 00:05:52,020
If I were to use var here.

76
00:05:53,120 --> 00:05:55,890
OK remember the global scope it's one with LET.

77
00:05:55,970 --> 00:05:57,360
If I use var.

78
00:05:57,530 --> 00:05:59,900
Look at that it changes to 10.

79
00:05:59,930 --> 00:06:08,210
OK so the loop the variable A in the loop since we use if our actually changes the value of the global

80
00:06:08,220 --> 00:06:10,500
A which is not good.

81
00:06:10,640 --> 00:06:16,180
OK so that's that's another reason that leten cons two are created.

82
00:06:16,580 --> 00:06:25,540
So hopefully that gives you an understanding of scope and how leten Konst are different than far left

83
00:06:25,710 --> 00:06:31,220
and Konst have a block level scope and var has a function scope.

