1
00:00:04,733 --> 00:00:07,733
Block scope was introduced in JavaScript

2
00:00:07,733 --> 00:00:11,400
in the ES 2015 release. Before we look at

3
00:00:11,400 --> 00:00:14,590
an example, let's clarify what exactly is

4
00:00:14,590 --> 00:00:17,533
a block. When we say block, we are

5
00:00:17,533 --> 00:00:20,333
referring to the code placed between curly

6
00:00:20,333 --> 00:00:22,483
braces. So with block scope, we are

7
00:00:22,483 --> 00:00:24,733
talking about the lifetime of variables

8
00:00:24,733 --> 00:00:26,733
within the curly brace of an if statement,

9
00:00:26,733 --> 00:00:29,933
a while, or a for loop, or any set of

10
00:00:29,933 --> 00:00:32,133
curly braces other than a function. Also

11
00:00:32,133 --> 00:00:34,933
note that the variables declared with the

12
00:00:34,933 --> 00:00:37,333
var keyword or created by function

13
00:00:37,333 --> 00:00:39,733
declarations do not have block scope.

14
00:00:39,733 --> 00:00:45,400
Examples will make this clear. We have an

15
00:00:45,400 --> 00:00:47,019
if statement that defaults to true. And

16
00:00:47,019 --> 00:00:49,097
inside the body of the function, we

17
00:00:49,097 --> 00:00:51,642
declare and set the value of count

18
00:00:51,642 --> 00:00:54,233
variable to 100. Next, we print the value

19
00:00:54,233 --> 00:00:58,133
of count to the console but get a syntax

20
00:00:58,133 --> 00:01:00,622
error. Why? The count variable was

21
00:01:00,622 --> 00:01:02,733
declared inside the if block, and as soon

22
00:01:02,733 --> 00:01:05,133
as the code block execution completed, it

23
00:01:05,133 --> 00:01:08,162
went out of scope. This is similar to the

24
00:01:08,162 --> 00:01:10,733
last example. Here we are declaring

25
00:01:10,733 --> 00:01:13,370
another variable with the same name as the

26
00:01:13,370 --> 00:01:15,590
variable before the if block and setting

27
00:01:15,590 --> 00:01:19,067
it to Inside if block. Then we lock the

28
00:01:19,067 --> 00:01:22,233
message variable, and it prints inside if

29
00:01:22,233 --> 00:01:24,233
block. But when we print the message

30
00:01:24,233 --> 00:01:26,400
variable again, outside the if block, it

31
00:01:26,400 --> 00:01:29,178
prints Hello. So as you can see, we're

32
00:01:29,178 --> 00:01:31,844
allowed to reuse variable names within

33
00:01:31,844 --> 00:01:34,162
code blocks, but they override the value

34
00:01:34,162 --> 00:01:36,400
of message while the block is being

35
00:01:36,400 --> 00:01:37,983
executed. Once the execution moves outside

36
00:01:37,983 --> 00:01:41,305
of the block, the value is still what it

37
00:01:41,305 --> 00:01:44,305
was before the if block code execution

38
00:01:44,305 --> 00:01:48,233
started. Let's see this in action in our

39
00:01:48,233 --> 00:01:50,483
demo. In our first example, as we saw

40
00:01:50,483 --> 00:01:52,733
earlier, trying to access and print the

41
00:01:52,733 --> 00:01:55,333
value of count results in a reference

42
00:01:55,333 --> 00:01:58,533
error. However, if we change the type of

43
00:01:58,533 --> 00:02:02,400
count to var, then we get 100. This shows

44
00:02:02,400 --> 00:02:04,733
that count no longer has block scope.

45
00:02:04,733 --> 00:02:07,933
Therefore, it is always a good practice to

46
00:02:07,933 --> 00:02:12,162
use let instead of var to declare your

47
00:02:12,162 --> 00:02:13,000
variables. Next, we take a look at immediately invoked

