Check out a free preview of the full JS.Next: ES6 / ES2015 course:
The "Default Parameters" Lesson is part of the full, JS.Next: ES6 / ES2015 course featured in this preview video. Here's what you'd learn in this lesson:

Rather than data-proofing a function’s parameters, default values can be specified to ensure a parameter has a value even if there isn’t anything passed to the function. This allows for less code to be written and a more readable API. Default parameters can also be assigned with a method call.

Get Unlimited Access Now

Transcript from the "Default Parameters" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Aaron Frost: So, some other ways to refer to default parameters because they call them a million things and I don't know why. But when you go through the documentation that I don't know if they've called the same thing twice in the documentation. Default Arguments, Default Values, Devault Function Arguments, Default Optional Parameters, any similar combination of any to the words used here, or anything that you could possibly imply, also refers to the same topic.

[00:00:31] It's kind of a mess as far as the wording goes. And this is kind of a new version of an old seam, we've seen this before. In C++ we've got them, Python's got them, CoffeeScript's got them and now EcmaScript has got them too. So historically, like, we spend the first few lines of our code data proofing, right.

[00:00:50] And we've talked about all these things we do at the top of our functions, and data proofing's definitely one of them, right? Who's done this kind of stuff before, this kind of data proofing, right? Cuz we can't get down into the internals of the function, the lower lines, if we've got a random undefined floating through.

[00:01:05] And invariably like it once you hit you first bug, how do you fix the bug? You say like go to the very top of your function go down a line, and you do this. So even if you didn't write it that way the first time, maintenance mode kicks in and you do these kind of things.

[00:01:21] You kind of data proof yourself at the top of the function. So we spend the first and depending on what kind of function is it can be you know quite a few lines data proofing our stuff. So I call it data proofing that's not like it a word that you should pretend is real like that's just what I'm talking that's just me that's how I say it.

[00:01:42] So sometimes we'll use the short circuit expression some people will use a ternary example. It's up to you guys, you know, everyone's different, and that's fine. So here's kind of how you'd use it. So we have the sayHello thing, and we've got a name. And I'm going to give it a default of World.

[00:02:05] Okay, so this function, all it does is says hello to you, that's it. When you call sayHello, you got to give it a name, if you don't, it's gonna use world. Suffice it to say, hello diet dew, which there is none around here for some reason.
>> Speaker 2: Diet Coke.

[00:02:20]
>> Aaron Frost: Right? It's a little weird, but so yeah, so you say hello. You pass it Dt Dew, it's going to say hello there too. You pass an empty string, and it says hello and a space, okay. You pass it undefined or you don't pass anything which is the same as passing undefined and you can see that here in these last two examples.

[00:02:48] So, what you can tell from these four examples is passing in an empty string. So, it's not faulty, what triggers the assignment expression, okay. It's undefined that triggers it and some people like there was there's like actually big conversation in the TC 39. And they went back, I don't know if it like if they got physical, I don't think anyone got punched over it but it was like there was like serious conflict where they're like hey if I purposefully passed you on the find arguments.link, we won.

[00:03:20] And if arguments.linked is one end it's undefined, then I think you shouldn't trigger the default value sign, that means I wanted it to be undefined. And some people were like, well that's a silly use case. Like that's not a good use case for what we're doing here, so no.

[00:03:40] And that was kind of the generally, like that was a good opinion to have, that was a solid thing, if I purposefully pass it in, that's at least something to consider whether it's a good idea or not. At least they considered it and decided even if you purposely pass in undefined it's still not going to, it will still trigger it.

[00:03:58] Even though you meant, you're trying to put something there besides undefined when you're passing it. Anyway, so yeah, it's not just that arguments.length is not where you're at. As long as the thing is undefined it's gonna trigger that default assignment event. So empty string doesn't trigger it, and it's kind of important.

[00:04:24] Like again, I want you guys to know that [INAUDIBLE] is not gonna trigger obviously.
>> Aaron Frost: So what does it give me? It gets you less code, right, we talked about that, you're gonna be able to write less stuff. I think that code written with default parameters is easy to read.

[00:04:41] I showed you a couple of examples today. And just kind of cramming that stuff up in the method signature, for me, makes a lot of sense, it makes some good sense. And it also gives you some improved predictability. Like when you look at the function, I think having it in the signature, kind of tells you some of the stuff that's going to happen.

[00:05:00] Versus having to read through the body and see all the ternary code about what's going to happen based on if it is in the find, it's another thing. I think it improves the predictability as well. So you can, you don't deserve to sign it like to a static value, you can just assign it with a method call.

[00:05:21] So if I have a function that just logs the ID, and if I do use empty, I can call getRandom and it will go out and try to generate some random function, or some random number.
>> Speaker 3: Can you point it to a reference to a function, or does it have to be executed?

[00:05:43]
>> Aaron Frost: So it is actually a call at that, it's only gonna execute that method call just as if it was in line. So you don't pass a pointer to a function you pass it like what you want to call it then you know you are going to.
>> Speaker 3: I'm asking if you pass it a pointer, is it going to throw an error?

[00:06:00]
>> Aaron Frost: No I mean this is the same concept as like yeah he's straight on this is to ensure that I get the skins.
>> Speaker 4: And then that function's only gonna get called when that parameter is undefined.
>> Aaron Frost: Yeah, yeah, only on that trigger. Like the last slide, we talked about the triggers, how does it get triggered?

[00:06:21] And only on the trigger events is it going to actually call that.
>> Speaker 3: I was assuming that they called it immediately, and that it just cached value. Cuz it's set up like a function call and you're giving in the return value, right?
>> Aaron Frost: Yeah, yeah, yeah, yeah.
>> Speaker 4: It doesn't call once undefined?

[00:06:41] When you define x, it's not called once and always uses that value, it's called every time a is undefined when you call this.
>> Aaron Frost: Yeah, no, totally. You guys, let's write this out real quick. So what he's saying is he figured it would only call the default value once, as opposed to every single time through.