Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Finally we’re able to set default values for function parameters like every other programming language. We’ll review just how simple it is to use.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Another feature of VS2015
is default parameters.
0:00
Default parameters let you
set default values for
0:04
parameters accepted by a function.
0:06
If you're familiar with other
programming languages you're probably
0:08
already familiar with the concept.
0:11
You can call a function and
if you don't pass a value for
0:13
certain parameters the function can fill
in those blanks with the default values.
0:16
You can follow along using
the latest workspace by
0:21
launching the workspace on this page.
0:23
Suppose we have a function.
0:25
We'll call it greet.
0:27
It takes two parameters, name and time of
day, and it logs a message to the console.
0:28
Now in ES5, we could check the arguments
passed into the function and
0:34
set default values.
0:38
So for example we can say
name = name | | 'guil' and
0:40
time of day = time of day double pipe day.
0:48
So here the double pipe
checks the variable.
0:54
And returns the value of the variable.
0:56
If it's true, the otherwise it returns
the default value on the right side.
0:59
If I call this greet function and
run the file in the console.
1:06
I see the message Good Day, Guil!
1:12
In ES 2015 we can set default values for
1:14
the parameters we pass to a function
simply by adding an equal sign in a value.
1:18
So I'll delete the name and time of day
variables a nd values I just wrote.
1:23
And right up here,
the values we pass can be a string,
1:29
Boolean, object, array or a function.
1:33
So, in this case, let's give our
parameters strings for default values.
1:36
So we'll say name = Guil and
time of day = day.
1:40
So, now even when I don't
provide a name or time of day.
1:51
The council will still log.
1:56
Good Day, Guil to the console.
1:57
So, now let's try something different.
2:00
I'm okay with the default value for
a name but
2:02
I wanna provide my greeting
a different value for time of day.
2:06
To do this, I need to pass the function
undefined as the value for
2:11
the name parameter.
2:16
You see, parameters with a default
value are considered optional.
2:18
So we pass undefined to tell
the interpreter to use the default value.
2:23
And then I'll set the second
parameter to afternoon.
2:28
So now when I run the file in the console,
it logs the message, good afternoon Guil.
2:34
Why thank you, console.
2:39
Default parameters are easy
to start using, and
2:42
will help your code be readable and
maintainable in the future.
2:44
In the next video I'll teach you about
rest parameters and spread operators.
2:47
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up