From the course: Learning ECMAScript 6+ (ES6+)
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Setting default function parameters - JavaScript Tutorial
From the course: Learning ECMAScript 6+ (ES6+)
Setting default function parameters
- [Instructor] There might be situations where you want to provide default values for your functions parameters. And with modern JavaScript, you can. So let's create a really basic function that adds a couple of numbers together. The function add will take in X and Y and then just console log the addition of X and Y to our console. Now, if I call the add function without passing in any values, it's going to return not a number. So you could fix this by adding numbers to your parameters, or you could also add defaults in case they were not supplied. So I'm going to say X equals five, Y equals six, and this will log 11 to the console. Now, what happens if I do add numbers? If I do pass values to a function that already has default parameters defined. Well, not surprisingly, whatever we pass in is going to override whatever is the default. Let's take a look at another type of function called haveFun. So haveFun…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.