1 00:00:00,491 --> 00:00:04,570 JavaScript lets you assign default values to your function parameters. 2 00:00:04,570 --> 00:00:08,980 That way if for any reason you do not pass a certain argument to a function, 3 00:00:08,980 --> 00:00:11,590 the function uses or falls back to the default value. 4 00:00:12,670 --> 00:00:16,622 Open the file default-params.js, 5 00:00:16,622 --> 00:00:24,670 then update the script tag in index.html to point to default-params.js. 6 00:00:24,670 --> 00:00:27,910 Let's first take a look at what happens when you don't pass an argument 7 00:00:27,910 --> 00:00:29,260 to a function that requires one. 8 00:00:30,550 --> 00:00:35,840 I'll define a function named sayGreeting that takes the parameter name. 9 00:00:36,870 --> 00:00:39,050 I'm writing this as a function declaration, 10 00:00:39,050 --> 00:00:42,180 but you can also write this as an arrow function expression if you'd like. 11 00:00:42,180 --> 00:00:46,560 The function should return a greeting using the value of the name parameter. 12 00:00:46,560 --> 00:00:50,858 I'll set it to return a string using a template literal with the message, 13 00:00:50,858 --> 00:00:53,573 Good morning, followed by the value of name. 14 00:00:57,294 --> 00:01:01,820 The function now expects a name value passed to it as an argument. 15 00:01:02,870 --> 00:01:07,558 Over in the JavaScript console, let's see what happens if you call the sayGreeting 16 00:01:07,558 --> 00:01:10,145 function without passing it a name argument. 17 00:01:10,145 --> 00:01:12,430 The console prints Good morning, undefined! 18 00:01:13,530 --> 00:01:16,998 Undefined is one of JavaScript's built-in values. 19 00:01:16,998 --> 00:01:17,610 In this case, 20 00:01:17,610 --> 00:01:21,920 it means that there's no value assigned to the name parameter, it's undefined. 21 00:01:21,920 --> 00:01:26,220 In some cases, missing a function argument can break your entire program. 22 00:01:26,220 --> 00:01:30,350 To prevent any of that from happening, you can assign a default value to your 23 00:01:30,350 --> 00:01:33,320 parameters right inside the function definition. 24 00:01:33,320 --> 00:01:37,690 Let's assign the name parameter a default value using the equals sign, or 25 00:01:37,690 --> 00:01:41,090 the assignment operator, and set it to the string student. 26 00:01:42,360 --> 00:01:46,340 I'll save the file, and back in the console, I'll once again call 27 00:01:46,340 --> 00:01:49,850 the sayGreeting function without passing it a name argument. 28 00:01:49,850 --> 00:01:51,870 This time, the console prints Good morning, student! 29 00:01:53,400 --> 00:01:59,020 If you pass the function an argument, like the string Maria, 30 00:01:59,020 --> 00:02:02,060 now the value Maria gets assigned to the name parameter, and 31 00:02:02,060 --> 00:02:05,510 the function returns the greeting Good morning, Maria!, good. 32 00:02:05,510 --> 00:02:07,960 The default parameter acts as a fallback and 33 00:02:07,960 --> 00:02:11,640 safeguards your program from breaking or returning undefined. 34 00:02:13,330 --> 00:02:17,080 Now let's look at how default values work when you have multiple parameters in 35 00:02:17,080 --> 00:02:18,400 your function. 36 00:02:18,400 --> 00:02:21,140 I want to pass a custom greeting to the function 37 00:02:21,140 --> 00:02:23,630 instead of always displaying Good morning! 38 00:02:23,630 --> 00:02:27,350 So I'll add a greeting parameter to the function definition, 39 00:02:27,350 --> 00:02:29,420 then in the return statement, 40 00:02:29,420 --> 00:02:33,980 replace Good morning with the value that gets assigned to the greeting parameter. 41 00:02:36,300 --> 00:02:39,770 I'll save my file, and over in the console, 42 00:02:39,770 --> 00:02:45,080 I'll call the sayGreeting function, passing it a greeting argument only, 43 00:02:45,080 --> 00:02:50,880 like Hi there, the function returns Hi there, student! 44 00:02:50,880 --> 00:02:55,683 All right, now I'll assign a default value to the greeting parameter, 45 00:02:55,683 --> 00:02:57,663 I'll set it to Good morning. 46 00:03:01,374 --> 00:03:06,154 I'll test that this works by calling the sayGreeting function without passing it 47 00:03:06,154 --> 00:03:06,924 arguments. 48 00:03:09,436 --> 00:03:14,140 And it returns Good morning, student!, the default greeting and name. 49 00:03:14,140 --> 00:03:18,550 But what if you want to pass the function a name argument only? 50 00:03:18,550 --> 00:03:22,480 In other words, skip the greeting argument to use the default greeting and 51 00:03:22,480 --> 00:03:24,970 pass it just the name you want to display? 52 00:03:24,970 --> 00:03:27,920 Well, if you pass the function a name only, for 53 00:03:27,920 --> 00:03:33,460 example Guil, it's going to return Guil, student! 54 00:03:33,460 --> 00:03:36,650 Since the function's first parameter is greeting, 55 00:03:36,650 --> 00:03:40,250 it's assigning the string Guil to that parameter. 56 00:03:42,610 --> 00:03:45,737 Now, what if I just omit the argument, like this? 57 00:03:50,489 --> 00:03:52,200 This breaks the function. 58 00:03:52,200 --> 00:03:57,250 JavaScript throws a syntax error, it says Unexpected token, comma. 59 00:03:57,250 --> 00:04:01,080 Even if you've assigned a default value to that first parameter, 60 00:04:01,080 --> 00:04:04,340 the function expects you to pass it a value. 61 00:04:04,340 --> 00:04:08,119 In this case, you need to pass the function undefined as the value for 62 00:04:08,119 --> 00:04:09,500 the greeting argument. 63 00:04:15,039 --> 00:04:19,037 This instructs the JavaScript engine to use the default value assigned to 64 00:04:19,037 --> 00:04:20,540 the first parameter. 65 00:04:20,540 --> 00:04:23,800 It acts as a placeholder for the argument you want to skip. 66 00:04:23,800 --> 00:04:27,140 Calling the function now returns Good morning, Guil!, perfect. 67 00:04:27,140 --> 00:04:30,750 And keep in mind that you only need to pass undefined for all but 68 00:04:30,750 --> 00:04:32,140 the last argument. 69 00:04:32,140 --> 00:04:34,200 If the last argument is missing, 70 00:04:34,200 --> 00:04:38,560 the function will use the default value you set. 71 00:04:38,560 --> 00:04:41,870 All right, now that you've learned how to work with default parameters, 72 00:04:41,870 --> 00:04:46,120 why don't you revisit the getArea function you wrote earlier and 73 00:04:46,120 --> 00:04:49,010 assign the unit parameter a default value. 74 00:04:49,010 --> 00:04:52,620 You can copy this function snippet from the teacher's notes with this video. 75 00:04:52,620 --> 00:04:55,730 Let's say that based on where your site visitors live, 76 00:04:55,730 --> 00:04:59,540 you'll want your function to display square feet or square meters. 77 00:04:59,540 --> 00:05:01,740 If most of your visitors are from the US, 78 00:05:01,740 --> 00:05:04,530 you'll likely set the default to square feet. 79 00:05:04,530 --> 00:05:07,350 On the other hand, if most of your visitors are from Canada or 80 00:05:07,350 --> 00:05:10,180 Europe, for example, you'd set it to square meters.