1
00:00:00,600 --> 00:00:05,910
The last hope that we will talk about is called lexical scope.

2
00:00:05,939 --> 00:00:07,750
So what is laser scope?

3
00:00:07,770 --> 00:00:10,200
So let me comment this once.

4
00:00:11,340 --> 00:00:20,280
Now bring the function scope down here and change this one to less lexical scope.

5
00:00:20,310 --> 00:00:22,530
Okay, so save it.

6
00:00:22,530 --> 00:00:23,860
And there we go.

7
00:00:23,880 --> 00:00:25,290
Okay, a little bit.

8
00:00:25,290 --> 00:00:25,790
A little bit.

9
00:00:25,800 --> 00:00:26,420
A little bit.

10
00:00:26,430 --> 00:00:27,660
Yeah.

11
00:00:27,690 --> 00:00:28,150
Good.

12
00:00:28,170 --> 00:00:31,140
So what is lexical scope in JavaScript?

13
00:00:31,440 --> 00:00:39,390
It refers to the visibility of variables and functions in relation to the lexical structure of the code.

14
00:00:39,420 --> 00:00:41,330
That is the scope of a variable.

15
00:00:41,340 --> 00:00:45,690
A function is determined by H position in the code.

16
00:00:45,780 --> 00:00:52,590
For example, variables in function declared inside a function are only visible inside that function.

17
00:00:52,590 --> 00:00:53,730
That one we know.

18
00:00:53,820 --> 00:01:01,410
And again, lexical scope is an important concept in JavaScript because it affects how code is executed

19
00:01:01,410 --> 00:01:03,600
and how variables are accessed.

20
00:01:04,050 --> 00:01:10,830
But this is also a complex topic again, so and there are a few different ways to think about it.

21
00:01:11,160 --> 00:01:12,510
But let me break it down.

22
00:01:13,500 --> 00:01:19,090
One way to think about lexical scope is in terms of the nesting of functions.

23
00:01:19,110 --> 00:01:26,700
I mean, we are going to nest a function together and see how we can access variables across these nested

24
00:01:26,700 --> 00:01:27,510
functions.

25
00:01:27,600 --> 00:01:34,620
So the scope of the inner function is said to be lexical and close by the scope of the outer function.

26
00:01:34,620 --> 00:01:42,090
And this means that the inner function has access to variables and functions declared in the outer function,

27
00:01:42,090 --> 00:01:44,050
but not vice versa.

28
00:01:44,070 --> 00:01:47,580
So let me prove you something here about lexical scope.

29
00:01:47,970 --> 00:01:56,040
For this one, we are going to next function together and see if we can access variables both the nested

30
00:01:56,040 --> 00:01:57,390
and the outer function.

31
00:01:57,650 --> 00:01:58,130
Great.

32
00:01:58,140 --> 00:02:00,240
So let's declare a function.

33
00:02:00,240 --> 00:02:02,760
Let's say my function one.

34
00:02:03,920 --> 00:02:04,550
All right.

35
00:02:04,550 --> 00:02:07,670
And dysfunction will not take anything great.

36
00:02:08,240 --> 00:02:12,530
And inside this function, let's also declare other functions inside.

37
00:02:12,530 --> 00:02:17,330
So function is going to be another function.

38
00:02:17,420 --> 00:02:23,060
Remember, because functions are first class citizens, we can pass a function, a function, and we

39
00:02:23,060 --> 00:02:24,950
can return a function from a function.

40
00:02:25,370 --> 00:02:30,170
After this one, we are going to dive more about how we can create a function.

41
00:02:30,890 --> 00:02:33,800
So let's say we have a function called another function.

42
00:02:33,800 --> 00:02:39,710
Being inside my function and I has that and this a perfect function call.

43
00:02:39,710 --> 00:02:47,210
So let's say that inside my outer function of my inner function, I'm going to declare a variable C.

44
00:02:47,240 --> 00:02:48,710
S, which represents.

45
00:02:48,710 --> 00:02:49,340
Hello.

46
00:02:50,250 --> 00:02:51,120
As that.

47
00:02:51,360 --> 00:02:57,150
And inside the honor function, I'm also going to declare a variable, say y.

48
00:02:57,180 --> 00:03:02,220
So let's say here, let's y also equal to let's say ten.

49
00:03:02,610 --> 00:03:03,390
Perfect.

50
00:03:03,960 --> 00:03:09,480
So let's try to console.log the x in my first function.

51
00:03:10,130 --> 00:03:12,680
Now let's call this function down here.

52
00:03:12,830 --> 00:03:16,400
It's going to be my function one if I save it.

53
00:03:16,430 --> 00:03:18,750
Now, you see, I have.

54
00:03:18,770 --> 00:03:19,460
Hello.

55
00:03:20,060 --> 00:03:23,240
So how can you also call this function?

56
00:03:23,810 --> 00:03:24,980
This function here?

57
00:03:25,890 --> 00:03:26,760
Dysfunction.

58
00:03:27,580 --> 00:03:29,650
Because it is his cope with this one.

59
00:03:29,650 --> 00:03:33,370
We can also call the function inside my first function.

60
00:03:33,370 --> 00:03:39,630
So call, function or invoke for short in work.

61
00:03:39,640 --> 00:03:44,710
So this one too let's console.log the y and let's see.

62
00:03:44,710 --> 00:03:50,930
So now see that this function has been called, but this one hasn't been called because we haven't invoked

63
00:03:50,930 --> 00:03:51,700
the function.

64
00:03:51,820 --> 00:03:55,810
So come down here, let's say another function, call it.

65
00:03:55,960 --> 00:03:56,710
Let's save it.

66
00:03:56,710 --> 00:04:01,990
Now I have the two functions being called, but here we go.

67
00:04:02,260 --> 00:04:05,710
Even though we have a function scope here.

68
00:04:06,160 --> 00:04:09,850
But inside this function we have two things going on.

69
00:04:09,850 --> 00:04:14,290
We have a variable here and we have another function here.

70
00:04:14,710 --> 00:04:24,490
So a variable that we declared outside this function which is X can be accessed inside my inner function.

71
00:04:24,580 --> 00:04:25,510
Have a look.

72
00:04:25,540 --> 00:04:30,970
If I try to console.log x, let's console.log x, k and let's see.

73
00:04:30,970 --> 00:04:33,160
Now everything works fine.

74
00:04:33,160 --> 00:04:37,210
Line number 54, which is this line of T four.

75
00:04:37,660 --> 00:04:38,770
So it works.

76
00:04:38,800 --> 00:04:48,910
But if I try to access this Y outside this function inside my outer function here, so let me change

77
00:04:48,910 --> 00:04:51,160
this one to Y now let's see.

78
00:04:51,160 --> 00:04:53,530
Now I get an error.

79
00:04:53,530 --> 00:04:57,190
So take note about this again.

80
00:04:57,460 --> 00:05:05,020
A variable that we declared outside the inner function can be declared or can be console.log or can

81
00:05:05,020 --> 00:05:08,770
be found or can be used inside the inner function.

82
00:05:09,250 --> 00:05:17,740
But a variable declared inside my inner function cannot be accessed outside the inner function.

83
00:05:17,890 --> 00:05:20,650
That's about the physical scope.

