Now, I wanna start
by taking another look at functions.
And for this, I'll add a brand new file here
in this empty project and I'll name it functions.js,
simply because here I wanna dive
into some more advanced function concepts.
Now, for all of that, let's start by adding a function.
Let's add a function which is maybe called, greetUser,
and in this function, I'll simply console log, Hi there!
So that's pretty straightforward, I would say.
Now, of course we learned that we can execute
such a function by calling it like this.
And if we would write this code,
open this integrated terminal here in Visual Studio Code
and we then run functions.js with the node command,
then we see, Hi there! here.
That's of course, nothing new,
these are things we already learned about.
Now, we also learned that functions can take parameters
so that we can call them with different values,
which are passed into the function and can be used there.
For example, here, we could accept the userName
as a parameter value in this function
so that we can pass in a username
when we call this function, for example, Max.
That's how we pass in a value for this function here.
So that is how we can use such a parameter
and pass in a value.
And now in this function,
we can use this parameter as a variable.
So like a variable that's defined
and only available in this function,
and we could, for example, change this text to Hi
and then add userName like this,
so that this functional will say, Hi userName.
And then we could add another string thereafter
where we re-add an exclamation mark
after adding the username in this string.
And now with that, if we execute this file,
we see, Hi Max!
That's of course also not something that is new.
Now, what is new,
is that we can also set default values
for those parameters to make them optional
and what do I mean by that?
Well, let's say greetUser should accept this parameter
so that we can call it like this,
but we also want to be able to call it
in a more general way so that we also can execute greetUser
without passing in a value for this parameter.
At the moment that's technically possible,
the code would not crash,
but it also wouldn't work as intended.
Instead, we now see Hi undefined down there.
And then we see undefined here as a value,
because if we call this function like this
and we don't provide a value for userName,
which is what we're doing here,
we're not providing a value,
then the default value for userName
will be a special JavaScript value of type undefined.
So this, by default is undefined.
That's of course not what we want here.
Hence, we can add a default value
that should be used instead
by adding an equal sign here in this parameter list,
and then a default value that we wanna use,
for example, user,
so that we at least say, Hi user instead of Hi undefined.
So you can add default values to parameters
by adding an equal sign in the parameter list
and then setting your default value here.
And that default value can be anything,
a string, a number, an object,
an array, whatever you need.
And with that, if you save this
and you re-execute this file,
now you instead say Hi user! down there,
because we are using that default value here.
Now, of course, if we do pass in more a specific value,
as we're doing it here when we first call the function,
that more specific value is used.
So then this more specific value
overrides this default value.
So this default value is not used in that case.
And that can be a very useful feature.
Now, it is worth noting that if you have a function
that accepts multiple parameters,
so let's say, if we also accepted a greetingPrefix here,
which could be hi or anything else,
then your default parameters always have to come at the end
of the function parameter list
after all the parameters that don't have a default.
So that here I can then, for example, say
greetingPrefix + wide space + userName and so on.
And the reason for that, is that,
if we add a default value,
those parameters in the end become optional,
because we can pass a value for them, but we don't have to.
Parameters without a default value are not really optional,
because if we don't pass in a value,
we get undefined as a default value
and we typically don't want that.
And you always need to list non-optional parameters first
and optional parameters thereafter.
That's something to memorize.
So that here we can, for example, say Hi max
or just Hello nothing here.
The non-optional parameter value is always provided.
The other value for username
is not always provided down here
when we call that function.
And with that, if I would execute this code here,
we see, Hi Max! and Hello user.
So that's a little bit of extra knowledge about functions
that you should keep in mind, because it can come in handy
when you are defining your own functions
that should be a bit more flexible.