1
00:00:00,470 --> 00:00:05,660
Welcome back to functions in this lesson, we're going to look at parameters and multiple arguments,

2
00:00:05,660 --> 00:00:11,090
so arguments are the parameters when they get passed into to the function and we look at them within

3
00:00:11,090 --> 00:00:12,260
the syntax of the function.

4
00:00:12,270 --> 00:00:13,670
So then they're known as arguments.

5
00:00:13,880 --> 00:00:14,810
So it's the same thing.

6
00:00:15,730 --> 00:00:22,090
Function parameters, so they're inside the parentheses and as we saw, that argument's received by

7
00:00:22,090 --> 00:00:26,260
the function when it's invoked, so we've got the parameter when it's coming in.

8
00:00:26,260 --> 00:00:29,830
And then also it's known as an argument when it's being invoked.

9
00:00:30,280 --> 00:00:32,710
So it's the same thing, but different names for it.

10
00:00:33,160 --> 00:00:35,890
The function can also have multiple parameters.

11
00:00:36,100 --> 00:00:38,260
So we can have parameter one, two, three.

12
00:00:38,410 --> 00:00:43,840
Notice that the comma separated and the structure is the same where we still have that code that's being

13
00:00:43,840 --> 00:00:44,590
executed.

14
00:00:45,100 --> 00:00:47,760
So you can also pass in default arguments.

15
00:00:48,010 --> 00:00:51,730
So there's a number of ways to do that and going to be exploring those as well.

16
00:00:51,850 --> 00:00:53,430
So we've got some examples here.

17
00:00:53,650 --> 00:01:02,320
So by default before now, with the newer version of JavaScript, we do have an option to have a parameter

18
00:01:02,320 --> 00:01:04,570
and equal to a value.

19
00:01:04,840 --> 00:01:06,330
So that can set the value.

20
00:01:06,580 --> 00:01:12,700
And before, if we want to be more compatible with older browsers, there's some other ways to do that

21
00:01:12,700 --> 00:01:20,530
as well, where we can do a conditional statement here so that if we if NUM two is undefined, then

22
00:01:20,530 --> 00:01:21,930
it will equal five.

23
00:01:21,970 --> 00:01:25,620
So we've got the value of num to or five.

24
00:01:25,630 --> 00:01:33,160
So this is another way to set a default typeof num two and if type of num to is undefined we use a territory

25
00:01:33,160 --> 00:01:33,760
operator.

26
00:01:33,770 --> 00:01:36,440
So there is a challenge for this lesson.

27
00:01:36,730 --> 00:01:43,000
So using your knowledge of the earlier lessons, create three different ways to set a default value.

28
00:01:43,150 --> 00:01:48,310
If a function is missing an argument value, they should all produce the same output.

29
00:01:48,670 --> 00:01:51,850
And we just showed you that there are three different ways to do that.

30
00:01:52,300 --> 00:01:56,650
So go ahead, pause the video, open up your editor and try it out.

31
00:01:56,860 --> 00:02:03,520
And that's also important here to practice passing in those arguments, taking those values and then

32
00:02:03,520 --> 00:02:05,710
making use of them within the code.

33
00:02:05,750 --> 00:02:09,850
So let's set up three different functions and we can call them whatever we want.

34
00:02:09,850 --> 00:02:11,800
In this case, we're going to call it out or one.

35
00:02:12,020 --> 00:02:14,050
So this one is going to take No.

36
00:02:14,050 --> 00:02:14,500
One.

37
00:02:14,740 --> 00:02:16,630
It's going to take number two.

38
00:02:16,750 --> 00:02:21,520
So just like on our example and we want to set a default value for number two.

39
00:02:21,760 --> 00:02:23,860
So we are using a more modern browser.

40
00:02:23,860 --> 00:02:30,980
So we do have access to use the default argument so we can set up with assigned value of five.

41
00:02:31,270 --> 00:02:40,510
So in this case, will output both those values as into the console so we can have no one can be equal

42
00:02:40,510 --> 00:02:44,410
to whatever the value that's being passed in of number one.

43
00:02:44,950 --> 00:02:51,250
And then the other one in the console is going to say, number two, it's going to be whatever value

44
00:02:51,250 --> 00:02:54,660
of number two is being passed, a few functions that are going to run.

45
00:02:54,970 --> 00:02:58,990
So in one case, we can pass in one hundred and one hundred.

46
00:02:58,990 --> 00:03:00,910
So we pass on both of the arguments.

47
00:03:01,270 --> 00:03:06,760
And then the other case, what we'll do is we'll just pass on one hundred and see what happens.

48
00:03:07,000 --> 00:03:08,290
And we got to still correct that.

49
00:03:08,300 --> 00:03:09,160
So that should be two.

50
00:03:09,170 --> 00:03:10,420
So let's see what the output is.

51
00:03:10,690 --> 00:03:15,490
So refresh, we're getting number one as one hundred, number two as one hundred.

52
00:03:15,610 --> 00:03:18,550
And in this case, we didn't pass in a second argument.

53
00:03:18,790 --> 00:03:21,120
But we are getting that default of five.

54
00:03:21,340 --> 00:03:24,550
So you could also do something where you've got a default for both of them.

55
00:03:24,700 --> 00:03:30,180
And if you're not passing in anything, then you can see that the result is the same.

56
00:03:30,190 --> 00:03:31,450
So you do have that option.

57
00:03:31,810 --> 00:03:37,310
So the other one that we want it to do is we want to do another function and we can call this one add

58
00:03:37,330 --> 00:03:38,140
or two.

59
00:03:38,290 --> 00:03:41,200
And this is going to take in those same two arguments.

60
00:03:41,770 --> 00:03:48,490
And this is four older versions of browsers that might not have this default argument value here, where

61
00:03:48,520 --> 00:03:55,950
we're going to use a conditional statement to check to see if NUM two is equal to a value.

62
00:03:55,960 --> 00:03:59,080
So if there is a value here, then it's just going to assign that value.

63
00:03:59,290 --> 00:04:06,160
If there's no value there, then it's going to sign the value of five to it and we can try that in this

64
00:04:06,160 --> 00:04:06,900
case as well.

65
00:04:06,940 --> 00:04:09,160
So just update this to number two.

66
00:04:09,160 --> 00:04:15,640
Number two, and in this case, we do need to pass the first argument and you can see that it sets a

67
00:04:15,640 --> 00:04:16,960
default for the second one.

68
00:04:17,230 --> 00:04:19,090
And we'll try one last one.

69
00:04:19,240 --> 00:04:26,050
And this is the one that's also more commonly used as well, where we actually check the type of number

70
00:04:26,050 --> 00:04:29,380
two, so do type of number two.

71
00:04:29,560 --> 00:04:37,030
And if the type is not equal to undefined, then all we do is we just output number two.

72
00:04:37,060 --> 00:04:38,890
So that means that there is a value there.

73
00:04:39,280 --> 00:04:44,040
Otherwise we're going to set a value of five and we'll try this one out as well.

74
00:04:44,050 --> 00:04:45,970
And I'll update this to 100.

75
00:04:46,120 --> 00:04:47,380
So we refresh it.

76
00:04:47,560 --> 00:04:53,680
And you see, in this case, we do pass one hundred and one hundred and then this one is equal to one

77
00:04:53,680 --> 00:04:57,590
hundred, and the second value is being set by default.

78
00:04:57,880 --> 00:05:00,820
So coming up, even more on functions coming up in the next lesson.
