Check out a free preview of the full Professional JS: Features You Need to Know course

The "Default Values" Lesson is part of the full, Professional JS: Features You Need to Know course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano explains that in JavaScript, function arguments are not mandatory and can be called with any number of arguments. He also demonstrates how to set default values for function parameters, making the code cleaner and more concise.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "Default Values" Lesson

[00:00:00]
>> Maximiliano Firtman: And the last part that I have here that is actually pretty straightforward and quickly on this ES6 recap is default values. It's actually pretty simple. And probably, you thought that it was part of JavaScript since the beginning, but it wasn't. Is that, for example, in this case, I have this function that calculates a tax, it receives a price and a tax, and then it calculates price with the tax.

[00:00:25]
And what happens if I don't define the tax? As I mentioned before, arguments in JavaScript are actually not mandatory. I mean, some people don't know that when you, I mean, I have this calculateTax function, that do you know that I can execute the function without any value? That's not the syntax or runtime error.

[00:00:45]
So if I have a function declared with two arguments, I can call that function with no arguments, or with 1 argument, with 2, or with 1,000 arguments. It doesn't matter for JavaScript because the arguments are actually kind of syntax sugar. It's always an array of data that I receive.

[00:01:07]
So if you wanna reinforce that, that's why a lot of developers like TypeScript, because the TypeScript compiler will actually give you an error, say, hey, hey, no, no, no, calculateTax next to arguments, and you didn't pass those arguments. And of course, you can get some warning from linters in JavaScript as well.

[00:01:26]
But it's perfectly fine from a JavaScript engine point of view. So what happens if I do have, for example, in this second example, second console, I do have a price but not a tax? Well, you check the undefined value for those variables that you define and you set the value, for example.

[00:01:48]
Well, now we can simplify this just by setting a default value. So then you don't need this, and it's cleaner. Okay, from ES6, we have default values. But anyway, it's still the same. If I don't pass the price, it works. I mean, if I run this, it works.

[00:02:05]
I don't see any. Of course, what happens if I do console.log? Well, it will do undefined plus undefined multiplied by 0.2. Because this one has a default value, this one will become undefined. If I set also default. But you, well, at least it's not undefined. The answer will be zero, maybe.

[00:02:26]
But you don't need to define default values for all the arguments. Okay, make sense? Cool, well, that's for now, for the ES6 part. We will see more on ES6 with collections, with async operations, the part that we haven't seen, but it's time to start talking about what's after ES6.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now