Instructor: I showed you rest parameters
in the last lecture, so now it's time
to talk about default parameters.
Default parameters are actually
a very straight forward concept.
We use them whenever we want one or more parameters
of a function to be preset, so we want them
to have a default value.
ES6 brings us default parameters, but there is also a way
of coding this in ES5, so let's start by doing that.
We haven't used function constructors in a long time,
so let's do that now and create a person, okay?
But we create, specifically, a Smith person,
so a person which is a part of the Smith family,
so let's call it SmithPerson, and we want it
to have a first name, a year of birth,
also a last name and a nationality.
All right, and so we say it, as usual,
should be the firstName that we receive.
The lastName is of course the last name.
The same for the yearOfBirth and the nationality.
All right, so let's now use that function constructor
and create a new person called John once more.
It's a new SmithPerson, let's say John, and 1990.
JavaScript actually allows us to call any function
without specifying all of the arguments.
In this case, we only specify first name and year of birth,
but leave the last name and nationality empty.
What JavaScript does, in this case,
is that it simply assigns undefined to these two parameters,
so they will be undefined, and we're gonna make use of that
in a second.
I just wanna show it to you here in the console, okay?
Let's take a look at John.
Here we see, so last name is undefined
and the nationality is undefined as well.
What if you wanted to add a default
for the last name and for the nationality?
Imagine that we always would call
this function constructor like this only
with the first name and year of birth and
that the last name and the nationality would be default.
We can actually do that and make use of the fact
that the arguments that we don't specify will be undefined.
We can then use an if statement
or something simpler like the ternary operator,
and simply say if the last name is undefined
then let the last name be Smith
and else it's simply last name, so nothing changes.
Let's think about it again.
The variable will be undefined
if we do not specify the last name, okay,
so in this case last name will become Smith.
That makes sense because we say
that it's a SmithPerson here, right?
In case that is not undefined, then nothing happens.
We can, actually, delete this part from here.
We'll now do the same thing with the nationality.
If the nationality is undefined,
then we want the nationality to be, let's say, American.
All right, and then after that this part of the code here
will execute and do its work, so let's see what happens now.
Okay, so we have a small bug here,
and the problem is, actually, that in a ternary operator
we also have to always specify the else part.
Let's just say that in this case it's lastName = lastName.
It doesn't make much sense,
but we have to put the else part here as well,
and so here let's say nationality is the nationality.
Let's now take a look at John again.
Yeah so his last name is now Smith,
and his nationality is American.
We only called the SmithPerson operator function constructor
with two arguments, but we still have all
of these four properties of the object well-defined
because we implemented something similar
to default parameters.
Now these can, of course, be overwritten.
Imagine that we have another person, which is Emily,
and Emily is John's sister, and she married
with someone else that moved to Spain, and so we can now,
of course, specify a new last name and a new nationality.
Let's say that we have, also, a SmithPerson.
She is called Emily.
Let's say born in 1983, but her last name is now
no longer Smith but Diaz, so she moved to Spain
and is now Spanish and no longer American.
This is gonna override the default parameters
of our function constructor, so if we now take a look
at Emily, of course, the defaults were overwritten,
and we now have the last name as Diaz
and the nationality as Spanish.
This is how we use something like default parameters in ES5.
Now we're ready to see how this actually works in ES6.
Which is a lot easier and straightforward.
In ES6 all we have to do is,
of course this part is the same, so it's a SmithPerson,
and we have a firstName, a yearOfBirth.
Now this is where we define the default parameter.
We say that the last name should be Smith,
and that the nationality should be American
if we do not specifically say something else.
Then, of course, this part here is the same.
I will just copy it, and that's it.
We don't need any of this part here.
All we need to do is to specify the default parameters
right here where we actually declare the parameters.
If you now run this, and take a look at John.
Then this is exactly what we had before
but in a better and easier way, so Smith again here,
of course, and he is American.
The same with Emily.
We overrode the defaults, and so her last name is Diaz,
and she is Spanish.
Rest and default parameters definitely makes it easier
to work with functions in JavaScript,
so they are a very welcome addition to the language.
Let's now move on.