1
00:00:00,420 --> 00:00:06,420
Let's talk about another type of scope, which is function scope.

2
00:00:06,689 --> 00:00:14,850
So as usual, let's have some enough room here and let me comment this once and bring the comment down

3
00:00:14,850 --> 00:00:15,640
here.

4
00:00:15,660 --> 00:00:19,500
And this one is going to be as a function scope.

5
00:00:21,990 --> 00:00:23,490
Come up here and save it.

6
00:00:23,520 --> 00:00:23,920
Great.

7
00:00:23,940 --> 00:00:27,120
So what is a function scope in JavaScript?

8
00:00:27,150 --> 00:00:32,070
Function scope refers to the visibility of variables within a function.

9
00:00:32,520 --> 00:00:37,540
If a variable is inside a code block, it means they are in a block scope.

10
00:00:37,560 --> 00:00:44,540
For example, if statement and if a variable is inside, a function is being regarded as a function

11
00:00:44,550 --> 00:00:45,090
scope.

12
00:00:45,210 --> 00:00:46,700
So let's see.

13
00:00:46,710 --> 00:00:49,670
So let's declare a function counts.

14
00:00:49,680 --> 00:00:51,690
Let's say my function.

15
00:00:52,980 --> 00:00:55,890
And then it go to error function this time around.

16
00:00:56,580 --> 00:01:00,990
And for this function, let's declare a variable.

17
00:01:01,020 --> 00:01:03,930
So let's say let x equal to.

18
00:01:04,319 --> 00:01:04,950
Hello?

19
00:01:06,070 --> 00:01:06,690
Great.

20
00:01:06,700 --> 00:01:13,480
So let me call my function here as my function is that when I save it not in display because you are

21
00:01:13,480 --> 00:01:16,930
not returning anything from this particular function.

22
00:01:17,380 --> 00:01:22,270
But if I console dot log x let's see.

23
00:01:22,270 --> 00:01:22,930
I see.

24
00:01:22,960 --> 00:01:30,130
Hello, i have access to x because this variable is in the functions code because it has been declared

25
00:01:30,130 --> 00:01:31,720
inside this function.

26
00:01:31,720 --> 00:01:34,060
So I can access it inside.

27
00:01:34,240 --> 00:01:42,280
But if I try to console.log the same x outside my function, let's see I get an error.

28
00:01:42,280 --> 00:01:43,780
Meaning this function.

29
00:01:43,780 --> 00:01:49,660
Sorry, this variable is not in the global scope but instead is in a function scope.

30
00:01:50,080 --> 00:01:57,530
And again, even for this one, if I try to console log x before it's been declared.

31
00:01:57,550 --> 00:01:58,330
Have a look.

32
00:01:59,300 --> 00:02:09,110
You see, I get x six that cannot access x before initialization because I call the x before initializing

33
00:02:09,110 --> 00:02:09,380
it.

34
00:02:09,410 --> 00:02:12,140
That's what you call function scope.

