Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Let's start this section about functions with one
of the easiest parts,
which is default parameters.
And the new section means new starter files.
So as always get yours
from the GitHub repository,
and then open them up
in your VS code.
So as always,
we already start with strict mode here.
But now let's talk
about default parameters.
So sometimes it's useful to have functions
where some parameters are set by default,
so that we then do not have to pass them
in manually if we don't want
to change the default.
But anyways, let's not talk
about default parameters.
So sometimes it's useful to have functions
where some parameters are set by default,
this way, we do not have to pass them in manually,
in case we don't want
to change the default.
Now in this section,
we're going to continue with the airline theme
that we started by the end of the last section.
And so let's now create a very basic booking function.
And we're going to do that starting
with the knowledge that we already had previously.
So without the default parameters first.
So just a normal function.
And into this function,
we need to pass in the flight number,
the number of passengers
and the
price.
And then let's simply use this data
to create an object and push that into some bookings array.
So booking
and now here, we can use the enhanced object literal syntax
that we learned about in the previous section.
So we cannot create an object with a flight number property
without having to do this, remember.
So we simply define a variable here,
and that will then create a property with this name,
and also the value that's in the variable.
And then let's just log it to the console.
And then let's just create an array out here,
which will contain these bookings.
So we start empty here,
and then we simply push them each time there is a booking.
So basically, for the airline,
to keep the bookings in some kind of system.
And this is all very fictional.
It's just to show you the default parameters in this case.
And speaking of that,
based on what we learned in the last section
about short circuiting,
how would we implement default parameters?
Well, let's start by calling this function
without specifying some parameters and see what we get then.
So create booking.
And now with just a flight number,
let's say
LH
123.
And now we need a new terminal as always.
So again, I hit tab here, too, too,
autocomplete the operation.
And indeed, here is our browser,
with our page already running.
Now, here's some kind of problem.
Oh, and that's because we call this booking
here the same as the array.
And so here it is then trying to push
this object into itself, basically.
So we need to create bookings here,
giving it a new name.
And so indeed,
now we get here the result of this object.
And now it works.
And here we see the result of this booking object.
And so what we wanted to see here was that the non
passengers and the price are set to undefined,
because we didn't specify them.
And so now we can use short circuiting to our advantage,
because we know that these are faulty values.
Okay.
And what I'm doing here is basically the old way
of setting default parameters,
just to remember how it would work before year six.
So let's say I wanted to set the number
of passengers to one by default.
So basically, we would reassign this parameter like this.
So non passengers is non passengers,
or
one,
which is the default value.
And this works because of the reasons that we learned
in the last section,
which is that whenever this is a falsie value,
which undefined is,
then the result of the whole operator here will be
this second operand.
So this value here,
in this case, one.
And now we can do the same thing with the price.
Let's say 199 is the default price.
And so if we reload this now, then indeed,
we get our default values here.
Okay, so coming from this part of the code.
However, this is a lot of ugly, boilerplate code.
And again, this is the ES5 way of doing it.
So I will take this out,
let me actually write, ES5 here
because now in ES6 here there is a better way.
So all you have to do is to write this
equals
one.
And so now this year will be the default value.
And the same for the price,
let's say 2199.
And now if we reload,
then we get the same result here.
And this year, of course,
looks a lot nicer in this a lot more intuitive to read.
Now, of course, we can override these defaults.
Otherwise, this wouldn't make much sense.
So let's try
LH 123.
And now for two passengers.
And,
let's say 800.
Now, of course, these values are the ones
that we specified here,
instead of the default values.
Now one thing that's really cool about default values
is that they can contain any expression.
For example, we could do times 1.2, for example,
then we would get this here.
Okay, but what's even more useful is that we can actually
use the values of the other parameters
that were set before it.
So here, we can now say
that the price should be calculated based
on a number of passengers.
All right.
So if we now do this,
create booking again,
just some random flat number here.
Let's say again, two,
but then with five,
we will get another value.
So indeed, you see 398.
So the number continues down here,
and then
995,
and so the price is now dynamically calculated by default,
based on this value we specified
and the number of passengers.
And again, this only works for parameters that are defined
in the list before this one.
So this would not work.
Because JavaScript basically specifies
these parameters in orders.
And as it reaches.
So right now, as it reaches the price,
it doesn't know about the number of passengers yet.
And so that wouldn't work.
Now, another thing that's important
to note here is that we cannot skip arguments here,
when we call the function.
So for example, let's say we wanted to leave the number
of passengers as the default value,
but then specify the price.
So we cannot do this.
So skipping the number of passengers now,
and then only specifying a new price.
So if we do this, then of course,
the number of passengers becomes 1000.
Because the second argument here will always be mapped
to the second parameter.
So if we wanted to leave this one here at the default,
there is a nice trick.
So the number of passengers,
we can simply set to undefined.
Now, right, and this works again,
because setting the parameter to undefined
is the same thing as not even setting it,
remember, so when we don't set a parameter,
so when we don't pass an argument into that parameter,
then it will take the value of undefined.
And so specifying undefined here,
is exactly the same.
And so this is how we basically skip a default parameter
that we want to leave at its default.
And so now as I reload it here,
you see that a number of passenger is still one
and delete the price is now 1000.
Okay, so yet another very nice addition to the language
in ES6 here, and pretty straightforward to understand, too.
So see you in the next video.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.