WEBVTT 00:06.200 --> 00:10.880 In this video you're going to get introduced to block scoping in Essex. 00:10.880 --> 00:15.080 So in iOS 6 they introduced something called block scoping. 00:15.080 --> 00:17.320 So let's explore it. 00:17.390 --> 00:20.700 So let's create a. 00:20.990 --> 00:32.850 Or declare a variable Y and set it to two and then let's create a couple of code blocks. 00:32.890 --> 00:42.480 So a code block or a block is any code that you place inside curly braces like this you're gonna have 00:42.480 --> 00:44.060 to. 00:45.090 --> 00:47.830 And let's write some code inside the first one. 00:48.030 --> 00:59.120 So I'm going to declare a variable X and set it to one no in the second block I'm going to log X 01:03.400 --> 01:10.300 and if I save then I get x is most defined. 01:10.520 --> 01:23.570 And the reason why is because blocks in E6 they are separate when it comes to escape so when you declare 01:23.840 --> 01:32.420 variables using let's the scope of this block and of that block are completely separate. 01:32.660 --> 01:42.170 So we get an error if you try to access X inside this block because the best block can not communicate 01:42.260 --> 01:44.320 with that one. 01:44.330 --> 01:52.840 Now if you use Val instead you wouldn't get an error because variables declared using Val are not block 01:52.840 --> 01:57.200 scoped but let all variables declared using let. 01:57.310 --> 01:59.960 They are actually blocks kept. 02:00.310 --> 02:01.150 So you get an error 02:06.330 --> 02:14.560 now and it's declare another variable Z and set it to the first block. 02:14.580 --> 02:16.080 Now if you log Z 02:20.820 --> 02:33.280 you get to because Z is in the same block and you have access to the values it if you log it right there. 02:33.440 --> 02:34.850 So that's not a problem. 02:34.940 --> 02:38.440 Now if you declare Z again right the 02:41.320 --> 02:49.590 do you think you're gonna have an error because you're basically really clearing Z Well if I save I 02:49.590 --> 02:59.570 wouldn't get any error because these two variables they are in two separate blocks. 02:59.790 --> 03:04.120 So redecoration is not gonna cause any problems. 03:04.310 --> 03:13.140 And if you log Z in this one you're gonna get four because you're accessing the value of z in the same 03:13.140 --> 03:15.030 block. 03:15.200 --> 03:15.660 All right. 03:15.660 --> 03:27.120 So again no declaration error because variables are in different scopes 03:30.580 --> 03:30.830 All right. 03:30.830 --> 03:31.130 So 03:35.260 --> 03:38.270 now if you log 03:41.830 --> 03:42.250 Y 03:46.070 --> 03:47.660 do you think you're gonna get an error. 03:47.720 --> 03:55.130 Because we are inside a block and there is no why variable inside the block but there is a Y variable 03:55.130 --> 03:56.480 in the global scope. 03:56.480 --> 03:58.020 So let's find out. 03:58.460 --> 04:13.520 We get to because a block has got access to the global environment or its outer environments. 04:13.620 --> 04:18.180 In our case we have access to Y which is equal to two. 04:18.390 --> 04:23.140 So there is no problem logging y inside the block. 04:23.270 --> 04:23.500 All right. 04:23.510 --> 04:35.020 So this is a quick introduction to block scoping in E6 and it's very important because this is going 04:35.060 --> 04:36.790 to have many 04:39.270 --> 04:49.460 consequences when it comes to for loops and many other pieces of code where you're going to find blocks. 04:49.530 --> 04:54.760 So it's very important to muster block scoping in Essex. 04:54.990 --> 04:57.990 So a quick reminder about iOS 5 05:00.880 --> 05:07.100 fake scoping which we did using immediately invoked function expressions so 05:10.410 --> 05:14.170 it's called it's fake scoping. 05:14.170 --> 05:23.890 So instead of blocks we used to place these two pieces of code inside immediately invoked function expressions 05:24.100 --> 05:37.810 like this. 05:38.500 --> 05:39.230 Uh uh. 05:39.280 --> 05:41.710 They were completely separate 05:47.510 --> 05:52.280 creating two different scopes. 05:52.460 --> 05:53.310 All right. 05:53.400 --> 05:54.320 So it's common this. 05:54.800 --> 05:55.040 All right. 05:55.040 --> 06:04.250 Now let's play with some code where you would find blocks and see how blocks scoping is going to affect 06:04.430 --> 06:05.630 our results. 06:05.630 --> 06:14.570 So for example let's declare a variable Mark and set it to 80. 06:14.660 --> 06:16.640 And let's say that we're going to have 06:19.560 --> 06:20.310 a condition 06:23.500 --> 06:33.730 so if Mark is greater than 50 then we're going to have we're going to pass. 06:33.730 --> 06:36.940 So we're going to set a variable pass to 06:40.990 --> 06:46.470 otherwise we're going to set pass to false. 06:46.480 --> 06:51.880 So now if you log pass 06:54.730 --> 06:58.440 what do you think is going to happen. 06:58.450 --> 07:02.310 Are you're gonna get an error or are you gonna get true. 07:02.320 --> 07:05.380 Because Mark is greater than 50. 07:05.380 --> 07:06.890 Well let's find out. 07:07.000 --> 07:15.100 You get an error saying pass is not defined because buzz can be found inside two separate blocks. 07:15.100 --> 07:22.030 And we're calling pass in the global scope to the global scope does not have access to pass inside these 07:22.030 --> 07:23.350 box. 07:23.350 --> 07:24.460 So to fix this 07:32.660 --> 07:35.500 we're gonna need to change the structure of our code. 07:35.600 --> 07:49.200 We're going to need to declare parts in the global scope and then instead of declaring paths inside 07:49.200 --> 07:55.200 the scope or these blocks we're going to set its value to true or false. 07:55.200 --> 08:03.730 So now when you call pass right there you have access to the volume pass because you have declared is 08:03.730 --> 08:05.600 in inside the global scope. 08:06.340 --> 08:14.620 So let's try this time and you get to then this time you get false okay 08:23.460 --> 08:29.880 in the next video you're going to see how functions are gonna be affected with block scoping in S6.