1
00:00:05,320 --> 00:00:07,320
It's important that we understand the

2
00:00:07,320 --> 00:00:10,320
lifetime of variables within a function.

3
00:00:10,320 --> 00:00:12,945
In this example, we have a function

4
00:00:12,945 --> 00:00:14,987
greeting that simply declares a variable

5
00:00:14,987 --> 00:00:20,320
message and assigns the value Hello to it.

6
00:00:20,320 --> 00:00:22,320
We invoke the greeting function first.

7
00:00:22,320 --> 00:00:24,945
When we try to access the message

8
00:00:24,945 --> 00:00:26,749
variable, we get a reference error. This

9
00:00:26,749 --> 00:00:29,653
is because message has function scope. And

10
00:00:29,653 --> 00:00:31,987
as soon as the greeting function completes

11
00:00:31,987 --> 00:00:34,463
execution, the message variable is no

12
00:00:34,463 --> 00:00:36,653
longer available. In other words, message

13
00:00:36,653 --> 00:00:39,653
is now out of scope. Here's another

14
00:00:39,653 --> 00:00:42,195
example. We have our greeting function

15
00:00:42,195 --> 00:00:44,120
with message variable initialized through

16
00:00:44,120 --> 00:00:47,195
the string Hello. But we also have a

17
00:00:47,195 --> 00:00:49,120
nested function sayHi. This is a function

18
00:00:49,120 --> 00:00:52,320
expression in which the function hi is set

19
00:00:52,320 --> 00:00:55,520
to the variable sayHi. In the nested

20
00:00:55,520 --> 00:00:57,820
function body, we log out the message to

21
00:00:57,820 --> 00:01:00,195
the console. We add a statement invoking

22
00:01:00,195 --> 00:01:03,120
the sayHi function at the end of the body

23
00:01:03,120 --> 00:01:05,320
of the greeting function. Finally, we

24
00:01:05,320 --> 00:01:08,820
invoke the greeting function to start this

25
00:01:08,820 --> 00:01:11,070
execution. Notice that when we get to the

26
00:01:11,070 --> 00:01:13,320
line where the output is being redirected

27
00:01:13,320 --> 00:01:15,463
to the console, the message variable is

28
00:01:15,463 --> 00:01:18,098
still in scope even though it's not

29
00:01:18,098 --> 00:01:20,320
declared inside the nested function.

30
00:01:20,320 --> 00:01:22,034
Whenever a function cannot find a

31
00:01:22,034 --> 00:01:25,320
variable, it looks to its parent function,

32
00:01:25,320 --> 00:01:28,520
so it's able to find the message variable

33
00:01:28,520 --> 00:01:30,820
right here. Moreover, JavaScript will keep

34
00:01:30,820 --> 00:01:34,034
looking at a parent and its parent for

35
00:01:34,034 --> 00:01:36,320
deeply nested functions until it either

36
00:01:36,320 --> 00:01:39,820
finds the variable or you get a reference

37
00:01:39,820 --> 00:01:43,320
error. Let's look at one more example.

38
00:01:43,320 --> 00:01:46,320
Here, we have the same function as before.

39
00:01:46,320 --> 00:01:48,945
But this time, we declare another variable

40
00:01:48,945 --> 00:01:51,320
message inside the nested function sayHi.

41
00:01:51,320 --> 00:01:54,120
We set it to the values hi, close the

42
00:01:54,120 --> 00:01:57,320
nested function body with a curly brace.

43
00:01:57,320 --> 00:01:58,876
We then invoke the sayHi function before

44
00:01:58,876 --> 00:02:02,764
logging the value of message to the

45
00:02:02,764 --> 00:02:04,820
console. So you could choose the same

46
00:02:04,820 --> 00:02:06,987
variable name as available in the parent

47
00:02:06,987 --> 00:02:09,749
function, but it will go out of scope as

48
00:02:09,749 --> 00:02:12,764
soon as the nested function completes, and

49
00:02:12,764 --> 00:02:16,320
you get the value Hello in the console.

50
00:02:16,320 --> 00:02:18,820
Let's see function scope in action in

51
00:02:18,820 --> 00:02:21,653
Visual Studio Code. Here, we get a

52
00:02:21,653 --> 00:02:23,920
reference error since message variable is

53
00:02:23,920 --> 00:02:27,570
declared inside the greeting function. If

54
00:02:27,570 --> 00:02:29,070
we move its declaration outside the

55
00:02:29,070 --> 00:02:32,820
function, we can see its value in the

56
00:02:32,820 --> 00:02:35,820
console. In this example, we declare

57
00:02:35,820 --> 00:02:37,891
another message variable, which is

58
00:02:37,891 --> 00:02:39,695
completely different from the variable by

59
00:02:39,695 --> 00:02:42,653
the same name in the parent function. As

60
00:02:42,653 --> 00:02:44,987
discussed earlier, when we print its value

61
00:02:44,987 --> 00:02:47,445
outside the nested function, we get the

62
00:02:47,445 --> 00:02:50,606
value Hello and not Hi. However, if we

63
00:02:50,606 --> 00:02:53,000
override the message value, then we get Hi as we're referring to the same variable.

