WEBVTT

00:01.500 --> 00:06.870
Now it's time to consider another very helpful and convenient feature which was released in Essex,

00:07.260 --> 00:09.480
I'm going to talk about default parameters.

00:10.230 --> 00:13.140
Let's go to Brackett's and create function.

00:14.580 --> 00:15.870
Let's call that person.

00:17.700 --> 00:26.520
And insert here three parameters, first name, last name and profession.

00:28.880 --> 00:30.230
Then running council.

00:33.570 --> 00:37.290
Right, I am right, first name.

00:39.090 --> 00:48.810
Then last name, then the text, and I am and place your thought parameter profession.

00:51.460 --> 00:59.730
Then invoke this function and as an argument, let's place here on the first name, for example, John,

01:00.400 --> 01:05.440
so as you see, function has three parameters, but as arguments, we have only one.

01:06.340 --> 01:09.570
If we reload, we will get I am John.

01:10.120 --> 01:13.290
But as for a last name and profession we have here undefined.

01:14.290 --> 01:17.320
The reason is that we did not insert the rest of the arguments.

01:17.890 --> 01:24.550
JavaScript engine found here one argument and as a default, it has been represented as the first parameter

01:24.700 --> 01:25.330
first name.

01:25.540 --> 01:29.130
In order to fix that in ESPIED, we have several ways.

01:29.800 --> 01:36.550
First one is to use a statement and as a condition we have to place your last name.

01:37.120 --> 01:40.350
Three equals to undefined.

01:40.690 --> 01:45.910
So if this condition is true, we need to assign the value to last name parameter.

01:46.870 --> 01:48.280
For example, as Smith.

01:50.920 --> 01:56.860
The exact same thing we have to do for third parameter, which is profession, so creative statement

01:57.640 --> 02:03.340
as a condition, right profession to people equals to undefined.

02:05.800 --> 02:09.010
So, again, if this condition is true, then we need to right.

02:09.010 --> 02:14.410
Profession equals to, let's say, programer.

02:15.410 --> 02:19.850
So if we reload the page, we will get the results, what we wanted.

02:20.390 --> 02:22.990
I'm John Smith and I am programer.

02:23.270 --> 02:27.290
OK, now let's insert second and third arguments and rights.

02:27.290 --> 02:36.290
For example, though, as last name and then instructor as profession.

02:36.980 --> 02:38.000
Reload the page.

02:38.630 --> 02:41.600
And we have I'm John Doe and I am an instructor.

02:42.260 --> 02:45.460
In this case, the conditions in if statements were false.

02:46.160 --> 02:51.620
So the code inside them was executed and we got the result with a different last name and profession.

02:52.100 --> 02:54.380
So this is one way to fix that problem.

02:54.380 --> 03:02.180
But besides that, instead of if statements we can use logical or operator and achieve the same results,

03:03.290 --> 03:08.870
let's do it for professional parameter delete if statement and instead of it.

03:08.870 --> 03:16.220
Right profession equals to profession or programmer.

03:18.890 --> 03:25.690
This is kind of short wait and using your statement, so when we invoke function, if we insert their

03:25.700 --> 03:29.770
third argument, then parameter profession will take relevant value.

03:30.290 --> 03:35.120
But if we miss that parameter, then it will take as a value programmer.

03:36.170 --> 03:43.520
So reload the page and we have the same result because the third argument is defined, but if we delete

03:43.520 --> 03:43.790
it.

03:45.020 --> 03:48.890
Then we will get programer as a value of profession.

03:49.610 --> 03:53.660
All right, this is the way how we could work with parameters in S5.

03:54.110 --> 03:58.300
Honestly, it wasn't so bad, but ethics offers us much better way.

03:58.970 --> 04:07.310
We don't need if statements or logical or operators anymore, we can simply assign the default values

04:07.310 --> 04:09.360
to parameters when we create them.

04:09.980 --> 04:23.410
So right here, first name equals to John, then assign Smith to last name and for profession.

04:24.050 --> 04:26.150
Right programmer.

04:28.680 --> 04:31.080
Also, delete the arguments below.

04:33.950 --> 04:41.660
Revolt and we have default parameter values and George Smith and I am programer, so those are default

04:41.660 --> 04:43.490
parameters with default values.

04:44.000 --> 04:54.890
But of course, if we insert here arguments, for example, as first name Niek, that though as last

04:54.890 --> 05:04.190
name and instructor for profession, then they will be overwritten to default values and will get I

05:04.190 --> 05:06.740
am Nito and I am instructor.

05:08.180 --> 05:11.800
So this is the main use case of default parameters in ethics.

05:12.200 --> 05:15.520
I'm not going to go further and write some crazy examples.

05:16.040 --> 05:19.310
I think it's quite enough to be able to use them in future.

05:19.970 --> 05:21.050
So let's move on.
