WEBVTT 00:06.050 --> 00:12.470 In this chapter you're going to learn new ways of declaring variables in E6 and how this would affect 00:12.590 --> 00:14.900 scope and hoisting. 00:14.960 --> 00:23.870 Now before we dive in let's have a quick review of the year fives var declaration its scope and hoisting 00:24.500 --> 00:24.920 so 00:27.710 --> 00:30.660 in is 5. 00:30.910 --> 00:39.100 We used to declare variables using var mainly we had also concerts but we're gonna come back to concerts 00:39.100 --> 00:39.820 later. 00:39.970 --> 00:43.930 So let's declare a variable X and set it to 1. 00:43.930 --> 00:51.440 Now I'm a variable x is gonna be global scoped because we are declaring it in the global scope outside 00:51.680 --> 00:53.150 any function. 00:53.150 --> 01:00.220 Now we know that variables were global scoped or function scoped. 01:02.420 --> 01:12.320 So if you declare a variable or a function at 1 for example and you declare a variable X inside it then 01:12.350 --> 01:22.900 you know that this variable here is a function scoped unlike the first one which is global script. 01:22.970 --> 01:36.910 Now if you return something like Y plus X and you try and one of three then you know you're gonna get 01:36.910 --> 01:37.500 s 5. 01:38.160 --> 01:45.030 And that's because when you get there and you look for the value of X first of all you look for the 01:45.030 --> 01:52.620 value of x inside the execution context of your function and that's where you find X equal to 2 right 01:52.620 --> 01:54.950 there and you use the value to. 01:55.180 --> 02:07.080 Now if you create another function and 2 and you do not declare a variable X inside a 2 and you try 02:09.620 --> 02:13.440 to 0 3 then you're gonna get 4. 02:13.620 --> 02:18.720 And that's because when you get away that and you look for the value of x inside the execution context 02:18.780 --> 02:21.560 of your function are two you don't find it. 02:21.720 --> 02:29.360 Then you go to the outer environments of the function or its execution context and you find X in the 02:29.390 --> 02:31.940 global scope and then you use the value 1. 02:32.060 --> 02:35.690 And that's why you get 4 which is 3 plus 1. 02:36.890 --> 02:45.050 Finally we know that variables in is 5 were hoisted 02:47.930 --> 02:49.960 when you declared them using 1. 02:50.440 --> 02:59.090 So if you log for example a variable Z before you declare it you wouldn't get an error while you get 02:59.150 --> 02:59.890 undefined. 03:01.710 --> 03:10.730 And the reason why is because all the variables declared using var they are hoisted at the beginning 03:10.850 --> 03:13.370 before the execution of you of your code. 03:13.370 --> 03:20.790 So before you code gets executed you know that z is going to be hoisted and set to undefined. 03:20.790 --> 03:23.250 Now when you get to that line 03:26.090 --> 03:29.270 you haven't said Z yet. 03:29.270 --> 03:37.650 So you're gonna use the value undefined which was set to when it was hoisted so no error. 03:37.650 --> 03:46.350 Thanks to hoisting now if you log Z at the end you're gonna get one this time. 03:46.350 --> 03:49.790 So this was a quick overview of the. 03:49.830 --> 03:51.660 Var declaration. 03:51.700 --> 03:54.750 It's the scope and hoisting in Ian's wife.