1 00:00:00,530 --> 00:00:04,090 Another feature of VS2015 is default parameters. 2 00:00:04,090 --> 00:00:06,550 Default parameters let you set default values for 3 00:00:06,550 --> 00:00:08,740 parameters accepted by a function. 4 00:00:08,740 --> 00:00:11,460 If you're familiar with other programming languages you're probably 5 00:00:11,460 --> 00:00:13,370 already familiar with the concept. 6 00:00:13,370 --> 00:00:16,380 You can call a function and if you don't pass a value for 7 00:00:16,380 --> 00:00:19,949 certain parameters the function can fill in those blanks with the default values. 8 00:00:21,200 --> 00:00:23,740 You can follow along using the latest workspace by 9 00:00:23,740 --> 00:00:25,850 launching the workspace on this page. 10 00:00:25,850 --> 00:00:27,160 Suppose we have a function. 11 00:00:27,160 --> 00:00:28,390 We'll call it greet. 12 00:00:28,390 --> 00:00:34,690 It takes two parameters, name and time of day, and it logs a message to the console. 13 00:00:34,690 --> 00:00:38,950 Now in ES5, we could check the arguments passed into the function and 14 00:00:38,950 --> 00:00:40,480 set default values. 15 00:00:40,480 --> 00:00:48,150 So for example we can say name = name | | 'guil' and 16 00:00:48,150 --> 00:00:54,135 time of day = time of day double pipe day. 17 00:00:54,135 --> 00:00:56,685 So here the double pipe checks the variable. 18 00:00:56,685 --> 00:00:59,325 And returns the value of the variable. 19 00:00:59,325 --> 00:01:03,575 If it's true, the otherwise it returns the default value on the right side. 20 00:01:06,855 --> 00:01:12,110 If I call this greet function and run the file in the console. 21 00:01:12,110 --> 00:01:14,460 I see the message Good Day, Guil! 22 00:01:14,460 --> 00:01:18,300 In ES 2015 we can set default values for 23 00:01:18,300 --> 00:01:23,100 the parameters we pass to a function simply by adding an equal sign in a value. 24 00:01:23,100 --> 00:01:29,140 So I'll delete the name and time of day variables a nd values I just wrote. 25 00:01:29,140 --> 00:01:33,430 And right up here, the values we pass can be a string, 26 00:01:33,430 --> 00:01:36,390 Boolean, object, array or a function. 27 00:01:36,390 --> 00:01:40,620 So, in this case, let's give our parameters strings for default values. 28 00:01:40,620 --> 00:01:47,867 So we'll say name = Guil and time of day = day. 29 00:01:51,056 --> 00:01:56,160 So, now even when I don't provide a name or time of day. 30 00:01:56,160 --> 00:01:57,460 The council will still log. 31 00:01:57,460 --> 00:02:00,890 Good Day, Guil to the console. 32 00:02:00,890 --> 00:02:02,990 So, now let's try something different. 33 00:02:02,990 --> 00:02:06,830 I'm okay with the default value for a name but 34 00:02:06,830 --> 00:02:11,730 I wanna provide my greeting a different value for time of day. 35 00:02:11,730 --> 00:02:16,950 To do this, I need to pass the function undefined as the value for 36 00:02:16,950 --> 00:02:18,360 the name parameter. 37 00:02:18,360 --> 00:02:23,080 You see, parameters with a default value are considered optional. 38 00:02:23,080 --> 00:02:28,640 So we pass undefined to tell the interpreter to use the default value. 39 00:02:28,640 --> 00:02:31,700 And then I'll set the second parameter to afternoon. 40 00:02:34,090 --> 00:02:39,420 So now when I run the file in the console, it logs the message, good afternoon Guil. 41 00:02:39,420 --> 00:02:40,750 Why thank you, console. 42 00:02:42,070 --> 00:02:44,610 Default parameters are easy to start using, and 43 00:02:44,610 --> 00:02:47,940 will help your code be readable and maintainable in the future. 44 00:02:47,940 --> 00:02:51,380 In the next video I'll teach you about rest parameters and spread operators.