1
00:00:00,360 --> 00:00:03,630
We're come to advanced functions.

2
00:00:03,990 --> 00:00:13,200
The first concept that we would talk about is what we call functions scope, understanding functions.

3
00:00:13,200 --> 00:00:23,070
Cope will help you to understand when your function works and when it doesn't work, or when your code

4
00:00:23,070 --> 00:00:26,720
works and when your code doesn't work.

5
00:00:26,730 --> 00:00:28,560
So let's go start it.

6
00:00:28,800 --> 00:00:37,080
When we say function, scope or scope for short, it simply means variable and functions visibility.

7
00:00:37,470 --> 00:00:44,820
Again, in layman's understanding of scope, it simply means that where can I find my variables or where

8
00:00:44,820 --> 00:00:46,020
can I find my function?

9
00:00:46,020 --> 00:00:52,470
That I've created that scope for you and we have different types of them, like I said.

10
00:00:52,470 --> 00:00:58,740
So let's explore more to differentiate between different scope in JavaScript.

11
00:00:59,160 --> 00:00:59,670
Great.

12
00:00:59,670 --> 00:01:08,250
So let's say that this is our code that we have written and this code we have a variable being declared

13
00:01:08,250 --> 00:01:16,470
here and we have an entire function being also declared here and inside my functions we also have some

14
00:01:16,470 --> 00:01:20,790
variables and if statement be declared inside.

15
00:01:21,150 --> 00:01:28,530
So for this code, each of these variables and functions has their own scope, meaning where they can

16
00:01:28,530 --> 00:01:29,280
be found.

17
00:01:29,640 --> 00:01:34,740
So the first hope is what we call global scope.

18
00:01:35,610 --> 00:01:43,650
In JavaScript, global scope refers to the root scope of the program, and global scope is a default

19
00:01:43,650 --> 00:01:49,890
scope for variables and functions defined outside of any other scope.

20
00:01:50,160 --> 00:01:58,290
This means that variables and functions defined in the global scope are accessible from anywhere in

21
00:01:58,290 --> 00:01:59,100
the program.

22
00:01:59,700 --> 00:02:07,950
Again, global scope is also created when your JavaScript code is loaded into the browser and variables

23
00:02:07,950 --> 00:02:13,680
and functions, is defined in the global scope, are accessible anywhere in your program.

24
00:02:14,130 --> 00:02:21,450
So this can be very useful when trying to create utility functions or variables that needs to be accessed

25
00:02:21,450 --> 00:02:23,700
from different parts of the program.

26
00:02:23,700 --> 00:02:31,680
But however, it can also lead to problems if variables or functions in a global scope are accidentally

27
00:02:31,680 --> 00:02:33,480
overwritten or modified.

28
00:02:33,480 --> 00:02:40,800
And we will talk more about this in the upcoming video with code and see the problems about declaring

29
00:02:40,800 --> 00:02:44,850
variables in a global scope that is scope for you.

30
00:02:45,000 --> 00:02:49,050
And after that you also have what is called look scope.

31
00:02:49,050 --> 00:02:56,910
So when you say look at scope, as the name implies, the variables that we declare are locally inside

32
00:02:56,910 --> 00:02:57,900
the function.

33
00:02:58,050 --> 00:03:05,910
So the function that we have created has its own scope, meaning that there are so we can have access

34
00:03:05,910 --> 00:03:11,400
to this variable inside this function scope here called local scope.

35
00:03:12,260 --> 00:03:20,630
But if I tried to access this particular array outside my function, because this is locally inside

36
00:03:20,630 --> 00:03:28,550
my function, I cannot access outside the function, meaning the variables are locally hosted inside

37
00:03:28,550 --> 00:03:29,510
the function.

38
00:03:29,720 --> 00:03:32,300
That is what you call look scope.

39
00:03:32,480 --> 00:03:37,060
And apart from that, you also have what is called function scope.

40
00:03:37,070 --> 00:03:39,290
So what is function scope?

41
00:03:39,650 --> 00:03:40,400
Well.

42
00:03:40,490 --> 00:03:46,010
Function scope refers to the visibility of variables within a function.

43
00:03:46,370 --> 00:03:52,490
Variables that are declared within a function are only accessible within that function.

44
00:03:52,520 --> 00:04:00,620
This means that if you try to access a variable that is declared inside a function from outside the

45
00:04:00,620 --> 00:04:03,200
function, you're going to get an error.

46
00:04:03,710 --> 00:04:11,150
So function scope is very important to understand because it can impact the way your code runs and when

47
00:04:11,150 --> 00:04:12,080
it doesn't run.

48
00:04:12,410 --> 00:04:19,339
For example, if you have a variable that is declared inside a function that is, let's say line number

49
00:04:19,339 --> 00:04:25,880
four, as I said, line number four, let's say count is equal to a R and my array.

50
00:04:27,460 --> 00:04:32,200
You would not be able to access that function or variable outside the function.

51
00:04:32,200 --> 00:04:34,630
And I will show you just a second with some code.

52
00:04:34,780 --> 00:04:35,170
Right.

53
00:04:35,170 --> 00:04:41,890
So this can be very useful if we want to have a certain piece of code to run under certain conditions.

54
00:04:41,890 --> 00:04:46,990
However, it can also lead to errors if you are not careful.

55
00:04:47,140 --> 00:04:53,710
So when we are working with Function's code, it's very important to keep track of your variables and

56
00:04:53,710 --> 00:05:01,180
where they are declared, and this will help you to avoid error and make your code more clean.

57
00:05:01,540 --> 00:05:04,660
That is about the function scope.

58
00:05:04,930 --> 00:05:09,760
And we also have another scope which is called block scope.

59
00:05:10,540 --> 00:05:18,610
In JavaScript, BlueScope refers to the visibility of variables within a code block.

60
00:05:18,850 --> 00:05:24,850
So you can see that for this particular function this is our code block because we have for the score

61
00:05:25,150 --> 00:05:29,380
basis here and here, this is what we call code block.

62
00:05:29,530 --> 00:05:37,000
A code block is a set of carry braces that encloses one or more statements because now we have what

63
00:05:37,000 --> 00:05:43,000
is called opening and then closing here and inside is my block scope.

64
00:05:43,510 --> 00:05:50,470
So if you use if statements and any loop logic like for loop and other loop, we have what is called

65
00:05:50,470 --> 00:05:51,940
a block scope.

66
00:05:52,390 --> 00:05:52,750
Great.

67
00:05:52,750 --> 00:06:01,390
So is declared within a code block are only visible within that block and are not accessible from outside

68
00:06:01,390 --> 00:06:02,110
the block.

69
00:06:02,440 --> 00:06:11,380
And one of the most common uses of plus code is with if statement and for loop and other loop as well.

70
00:06:11,680 --> 00:06:18,940
So the code block associated with an if statement is only executed if the condition evaluates to true,

71
00:06:19,030 --> 00:06:25,450
this means that variable is declared within the code block will only be accessible within the if statement.

72
00:06:25,540 --> 00:06:26,710
That is good, right?

73
00:06:26,710 --> 00:06:34,660
So overall block scope is a way of limiting the visibility of variables to only the code block in which

74
00:06:34,660 --> 00:06:35,740
they are declared.

75
00:06:35,860 --> 00:06:44,110
And this can be very helpful in preventing namespace collisions and making code more readable.

76
00:06:44,470 --> 00:06:50,620
In the next video, let's take these code variables and demonstrate with real code.

