WEBVTT 00:06.160 --> 00:08.970 In this video you gotta learn how to use defaults. 00:08.980 --> 00:12.610 Parameter values in E6 functions. 00:12.610 --> 00:22.930 So first let's go back to East 5 and see how we manage to use defaults parameters so we gonna create 00:22.960 --> 00:35.050 a function increment which takes a couple of parameters X and Y then he's going to return x plus y if 00:35.170 --> 00:38.800 y is available or provided. 00:38.800 --> 00:44.520 Now if y is not provided we're going to assign the value 1 2 y. 00:45.010 --> 00:46.690 All right so we're going to return 00:51.970 --> 00:55.200 x plus y. 00:55.370 --> 00:56.160 Right. 00:56.360 --> 01:00.200 But if y is not available we're going to need to return x plus 1. 01:00.230 --> 01:06.550 So we're gonna check if y is 20 point. 01:06.770 --> 01:12.290 So if that's the case we can return x plus 1. 01:12.290 --> 01:17.930 Otherwise we're going to return x plus y like this. 01:17.930 --> 01:18.670 All right. 01:18.680 --> 01:19.790 So now if you try 01:22.970 --> 01:24.200 increments of 01:26.820 --> 01:31.780 5 it's going to return 6 y is not available. 01:31.840 --> 01:40.380 So you only use the value 1 if you try increment of 5 and 3 then you gonna get the sum of 5 and 3. 01:40.570 --> 01:47.580 All right so that's how we used to do to do this in E5 so that looks a little bit laborious. 01:47.680 --> 01:50.270 Right now it is 6. 01:50.380 --> 01:56.320 Things are a lot easier now what we can do is create a function 01:59.120 --> 02:01.170 increment 2. 02:02.010 --> 02:04.360 You can use a function declaration if you like. 02:04.370 --> 02:14.040 So now we're we to use an arrow function and we can replace the prime which is inside the parentheses 02:14.790 --> 02:15.150 right. 02:15.990 --> 02:26.010 And we are returning x plus y but if y is not available we're gonna use a default value. 02:26.490 --> 02:29.160 And you can set the default value right here. 02:30.850 --> 02:31.190 All right. 02:31.200 --> 02:32.980 So what are we saying here. 02:33.200 --> 02:34.620 Can they return x must y. 02:35.210 --> 02:40.660 And if y is not provided then we gonna set it to one. 02:40.680 --> 02:41.760 So now if you try 02:48.680 --> 02:55.140 increment 2 of 5 you get 6 increments you 5 and 3 you get 8. 02:55.280 --> 02:57.140 Okay. 02:57.140 --> 03:06.950 Now what if you use restructuring structuring so suppose that you've got a function greed which takes 03:06.950 --> 03:14.810 three off a couple of elements first name and last name and we'll just log 03:18.130 --> 03:19.230 Hello. 03:19.740 --> 03:21.500 First they must be like this 03:24.440 --> 03:25.460 we're using here. 03:25.510 --> 03:26.440 String rituals 03:33.490 --> 03:34.410 so destroy 03:42.230 --> 03:43.150 greets of 03:46.540 --> 03:49.450 Bill Gates for example and we get Hello. 03:49.780 --> 03:51.300 Bill Gates No. 03:51.430 --> 03:57.510 Walt if you want to use a default value for the last thing. 03:58.630 --> 04:01.960 So suppose that's the last name is not provided. 04:01.960 --> 04:05.960 So we want to leave this empty right. 04:05.980 --> 04:09.230 So we just gonna say that's an empty string. 04:09.530 --> 04:14.870 So if we don't provide the second elements inside the array we just get. 04:14.870 --> 04:16.420 Hello Bill. 04:16.660 --> 04:19.640 That's very similar to what we did there right. 04:19.660 --> 04:26.900 And this is using this structuring let's make things a little bit more complicated this time we're going 04:26.900 --> 04:35.990 to use this structuring with an objects so let's define a function like that. 04:36.130 --> 04:44.280 So this function will take an object right with the properties item 04:47.460 --> 04:49.080 and price 04:51.800 --> 04:56.960 and quantity. 04:57.830 --> 05:08.910 So as you know we can reduce this to item price quantity without having to specify the value each time. 05:09.010 --> 05:12.940 So let's say that this function is just going to log something like 05:16.280 --> 05:26.060 please pay the price for the number of items. 05:26.210 --> 05:26.650 So 05:30.620 --> 05:39.230 the price or the cost is going to be price multiplied by quantity All right. 05:39.650 --> 05:40.100 And 05:45.900 --> 05:48.290 I'm going to halve the number of items 05:51.260 --> 05:52.280 which is the quantity 05:55.000 --> 06:04.880 and then the item they which is going to be item. 06:04.980 --> 06:08.970 All right so let's try this function. 06:10.260 --> 06:10.740 Way 06:15.830 --> 06:16.580 but it is 06:22.540 --> 06:25.830 let's say that the price is five 06:28.440 --> 06:34.360 and quantity five. 06:34.470 --> 06:36.640 What is their price is one. 06:37.050 --> 06:38.010 Let's see what we get 06:43.980 --> 06:44.980 what we seek. 06:45.300 --> 06:45.840 Let's try again. 06:45.870 --> 06:56.280 So please pay five or five bananas and let's just add a dollar right there so that we can see five dollars 06:56.430 --> 06:57.120 instead of five. 06:57.240 --> 07:00.200 So please pay five dollars for five bananas. 07:00.210 --> 07:00.690 Perfect. 07:01.140 --> 07:05.120 So now we want to implement default values for this function. 07:05.130 --> 07:15.390 So if the item is not provided we want to use a default value but now let's bring them. 07:16.110 --> 07:16.500 Right. 07:18.080 --> 07:22.190 So that's how he says the default value inside the objects. 07:22.220 --> 07:24.020 And the same thing happens with the price. 07:24.020 --> 07:32.460 So if it's not provided you as you just said that to zero the quantity as well is easy to zero. 07:32.480 --> 07:35.840 So now let's try again this time 07:39.730 --> 07:46.050 we will provide an empty object like this and let's see what happens. 07:46.190 --> 07:48.860 So please pay zero dollars or zero bananas. 07:48.950 --> 07:52.310 So we use the default values as expected. 07:53.240 --> 07:53.970 Okay. 07:54.050 --> 08:04.620 So now what happens if we don't provide any arguments inside our function. 08:04.660 --> 08:05.300 So what do you think. 08:06.480 --> 08:16.460 So let's save and see well we get an error very thing here can the structure property item of undefined 08:17.370 --> 08:20.380 must because our object doesn't exist. 08:20.390 --> 08:27.710 So it's undefined but it's still trying to find the property item of our objects. 08:27.710 --> 08:37.220 So to fix this you can set a default value for the whole objects but you see that you're seeing the 08:37.220 --> 08:47.080 parameter so to do that you set the whole object to a default value which is gonna be an empty object. 08:47.090 --> 08:50.770 So now the object is going to exist. 08:50.840 --> 08:51.190 All right. 08:51.290 --> 08:53.060 It's not gonna be undefined. 08:53.060 --> 09:01.340 So let's try now and this time it works and we've got our default parameters inside the objects. 09:01.340 --> 09:02.770 All right. 09:03.050 --> 09:08.620 Now let's simplify the syntax of our object which is using the problem. 09:09.140 --> 09:16.340 Since the names of the properties are the same as the variables we're using that we can just delete 09:17.090 --> 09:17.510 these 09:21.720 --> 09:22.200 like with 09:28.040 --> 09:31.960 all right to know when it's safe and make sure that it's still working. 09:31.950 --> 09:34.140 Perfect. 09:34.280 --> 09:34.580 All right. 09:34.590 --> 09:42.240 So this is a very important feature in E6 defaults by misses and it's going to make your programming 09:42.300 --> 09:44.580 experience almost easier. 09:44.580 --> 09:48.750 So in the next video we're going to move to arrest parameters.