- Now this is gonna be another really quick video
where I talk about how you can deal with
optional parameters in a function.
Let's say I have a function
that's gonna be print name and age.
It's gonna take in a name and an age.
So this age is a number
and the name here is going to be a string.
And we'll just define this function really quickly
as a simple console log of name and age.
Here we go.
What I can do is I can call print name and age
and I can pass it in a name and I can pass it in an age.
That works just fine.
But what happens if I want this age property to be optional?
'Cause in JavaScript, we know there's often times
where you want to create a function
where sometimes you pass it one parameter,
sometimes you pass a two,
sometimes you pass a three and so on.
And in our case, I want this age to be optional.
This is really common
if you have like an options argument.
For example, we have some options
and they're set to an object
with some different types inside of it.
That's a very common thing
that you would want to be optional inside a JavaScript,
because by default you just use the default values,
otherwise it's going to fall back
to actually using whatever values you pass to it.
What we can do is we can actually emulate that
by instead here just having our options.
And let's just say our options is going to be debug mode
and this is going to be a boolean, just like that.
So there's our option types.
What we can do is we can pass in here an object
that would have debug mode of true.
We can pass this in if we want,
but if we leave it off it's going to give us an error,
which is probably not something that we want.
So in our case, to make this optional
all we have to do is just put a question mark after it.
And that's just telling TypeScript,
hey, this is now an optional property.
Now what we can do inside of here
is we can pass it along if we want, as you can see here,
and it's going to work fine.
Otherwise we can leave it off,
and again, it's going to work just fine.