1
00:00:02,390 --> 00:00:03,770
Now, I wanna start

2
00:00:03,770 --> 00:00:07,410
by taking another look at functions.

3
00:00:07,410 --> 00:00:11,030
And for this, I'll add a brand new file here

4
00:00:11,030 --> 00:00:15,470
in this empty project and I'll name it functions.js,

5
00:00:15,470 --> 00:00:17,170
simply because here I wanna dive

6
00:00:17,170 --> 00:00:20,733
into some more advanced function concepts.

7
00:00:21,880 --> 00:00:25,410
Now, for all of that, let's start by adding a function.

8
00:00:25,410 --> 00:00:29,123
Let's add a function which is maybe called, greetUser,

9
00:00:30,500 --> 00:00:35,500
and in this function, I'll simply console log, Hi there!

10
00:00:36,530 --> 00:00:40,010
So that's pretty straightforward, I would say.

11
00:00:40,010 --> 00:00:42,320
Now, of course we learned that we can execute

12
00:00:42,320 --> 00:00:45,600
such a function by calling it like this.

13
00:00:45,600 --> 00:00:47,940
And if we would write this code,

14
00:00:47,940 --> 00:00:52,720
open this integrated terminal here in Visual Studio Code

15
00:00:52,720 --> 00:00:57,500
and we then run functions.js with the node command,

16
00:00:57,500 --> 00:00:59,680
then we see, Hi there! here.

17
00:00:59,680 --> 00:01:01,760
That's of course, nothing new,

18
00:01:01,760 --> 00:01:04,790
these are things we already learned about.

19
00:01:04,790 --> 00:01:09,260
Now, we also learned that functions can take parameters

20
00:01:09,260 --> 00:01:12,100
so that we can call them with different values,

21
00:01:12,100 --> 00:01:15,560
which are passed into the function and can be used there.

22
00:01:15,560 --> 00:01:20,020
For example, here, we could accept the userName

23
00:01:20,020 --> 00:01:23,240
as a parameter value in this function

24
00:01:23,240 --> 00:01:25,630
so that we can pass in a username

25
00:01:25,630 --> 00:01:29,070
when we call this function, for example, Max.

26
00:01:29,070 --> 00:01:34,070
That's how we pass in a value for this function here.

27
00:01:34,920 --> 00:01:37,820
So that is how we can use such a parameter

28
00:01:37,820 --> 00:01:39,920
and pass in a value.

29
00:01:39,920 --> 00:01:41,510
And now in this function,

30
00:01:41,510 --> 00:01:45,160
we can use this parameter as a variable.

31
00:01:45,160 --> 00:01:47,270
So like a variable that's defined

32
00:01:47,270 --> 00:01:49,860
and only available in this function,

33
00:01:49,860 --> 00:01:52,980
and we could, for example, change this text to Hi

34
00:01:52,980 --> 00:01:55,020
and then add userName like this,

35
00:01:55,020 --> 00:01:58,990
so that this functional will say, Hi userName.

36
00:01:58,990 --> 00:02:01,770
And then we could add another string thereafter

37
00:02:01,770 --> 00:02:04,370
where we re-add an exclamation mark

38
00:02:04,370 --> 00:02:07,323
after adding the username in this string.

39
00:02:08,759 --> 00:02:11,550
And now with that, if we execute this file,

40
00:02:11,550 --> 00:02:13,340
we see, Hi Max!

41
00:02:13,340 --> 00:02:16,750
That's of course also not something that is new.

42
00:02:16,750 --> 00:02:18,440
Now, what is new,

43
00:02:18,440 --> 00:02:22,630
is that we can also set default values

44
00:02:22,630 --> 00:02:26,690
for those parameters to make them optional

45
00:02:26,690 --> 00:02:28,580
and what do I mean by that?

46
00:02:28,580 --> 00:02:32,440
Well, let's say greetUser should accept this parameter

47
00:02:32,440 --> 00:02:35,190
so that we can call it like this,

48
00:02:35,190 --> 00:02:37,410
but we also want to be able to call it

49
00:02:37,410 --> 00:02:42,410
in a more general way so that we also can execute greetUser

50
00:02:42,520 --> 00:02:46,093
without passing in a value for this parameter.

51
00:02:47,150 --> 00:02:49,830
At the moment that's technically possible,

52
00:02:49,830 --> 00:02:51,980
the code would not crash,

53
00:02:51,980 --> 00:02:55,880
but it also wouldn't work as intended.

54
00:02:55,880 --> 00:03:00,880
Instead, we now see Hi undefined down there.

55
00:03:02,050 --> 00:03:04,740
And then we see undefined here as a value,

56
00:03:04,740 --> 00:03:07,430
because if we call this function like this

57
00:03:07,430 --> 00:03:10,680
and we don't provide a value for userName,

58
00:03:10,680 --> 00:03:11,810
which is what we're doing here,

59
00:03:11,810 --> 00:03:13,490
we're not providing a value,

60
00:03:13,490 --> 00:03:16,230
then the default value for userName

61
00:03:16,230 --> 00:03:21,230
will be a special JavaScript value of type undefined.

62
00:03:21,990 --> 00:03:24,973
So this, by default is undefined.

63
00:03:25,960 --> 00:03:28,520
That's of course not what we want here.

64
00:03:28,520 --> 00:03:30,600
Hence, we can add a default value

65
00:03:30,600 --> 00:03:32,370
that should be used instead

66
00:03:32,370 --> 00:03:36,800
by adding an equal sign here in this parameter list,

67
00:03:36,800 --> 00:03:39,360
and then a default value that we wanna use,

68
00:03:39,360 --> 00:03:41,600
for example, user,

69
00:03:41,600 --> 00:03:46,600
so that we at least say, Hi user instead of Hi undefined.

70
00:03:48,100 --> 00:03:51,370
So you can add default values to parameters

71
00:03:51,370 --> 00:03:53,960
by adding an equal sign in the parameter list

72
00:03:53,960 --> 00:03:57,250
and then setting your default value here.

73
00:03:57,250 --> 00:03:59,820
And that default value can be anything,

74
00:03:59,820 --> 00:04:01,930
a string, a number, an object,

75
00:04:01,930 --> 00:04:03,773
an array, whatever you need.

76
00:04:05,211 --> 00:04:07,070
And with that, if you save this

77
00:04:07,070 --> 00:04:09,630
and you re-execute this file,

78
00:04:09,630 --> 00:04:12,780
now you instead say Hi user! down there,

79
00:04:12,780 --> 00:04:17,382
because we are using that default value here.

80
00:04:19,290 --> 00:04:23,750
Now, of course, if we do pass in more a specific value,

81
00:04:23,750 --> 00:04:26,710
as we're doing it here when we first call the function,

82
00:04:26,710 --> 00:04:29,090
that more specific value is used.

83
00:04:29,090 --> 00:04:31,750
So then this more specific value

84
00:04:31,750 --> 00:04:34,440
overrides this default value.

85
00:04:34,440 --> 00:04:37,623
So this default value is not used in that case.

86
00:04:39,250 --> 00:04:42,370
And that can be a very useful feature.

87
00:04:42,370 --> 00:04:45,480
Now, it is worth noting that if you have a function

88
00:04:45,480 --> 00:04:48,280
that accepts multiple parameters,

89
00:04:48,280 --> 00:04:53,280
so let's say, if we also accepted a greetingPrefix here,

90
00:04:53,880 --> 00:04:56,520
which could be hi or anything else,

91
00:04:56,520 --> 00:05:01,240
then your default parameters always have to come at the end

92
00:05:01,240 --> 00:05:03,310
of the function parameter list

93
00:05:03,310 --> 00:05:07,420
after all the parameters that don't have a default.

94
00:05:07,420 --> 00:05:10,740
So that here I can then, for example, say

95
00:05:12,650 --> 00:05:17,650
greetingPrefix + wide space + userName and so on.

96
00:05:18,730 --> 00:05:20,860
And the reason for that, is that,

97
00:05:20,860 --> 00:05:23,520
if we add a default value,

98
00:05:23,520 --> 00:05:27,570
those parameters in the end become optional,

99
00:05:27,570 --> 00:05:30,893
because we can pass a value for them, but we don't have to.

100
00:05:31,890 --> 00:05:36,890
Parameters without a default value are not really optional,

101
00:05:37,690 --> 00:05:40,320
because if we don't pass in a value,

102
00:05:40,320 --> 00:05:42,620
we get undefined as a default value

103
00:05:42,620 --> 00:05:44,650
and we typically don't want that.

104
00:05:44,650 --> 00:05:48,140
And you always need to list non-optional parameters first

105
00:05:48,140 --> 00:05:50,400
and optional parameters thereafter.

106
00:05:50,400 --> 00:05:52,133
That's something to memorize.

107
00:05:53,220 --> 00:05:57,270
So that here we can, for example, say Hi max

108
00:05:57,270 --> 00:06:00,560
or just Hello nothing here.

109
00:06:00,560 --> 00:06:05,560
The non-optional parameter value is always provided.

110
00:06:06,890 --> 00:06:09,170
The other value for username

111
00:06:09,170 --> 00:06:11,570
is not always provided down here

112
00:06:11,570 --> 00:06:13,423
when we call that function.

113
00:06:14,950 --> 00:06:18,320
And with that, if I would execute this code here,

114
00:06:18,320 --> 00:06:20,693
we see, Hi Max! and Hello user.

115
00:06:21,910 --> 00:06:25,250
So that's a little bit of extra knowledge about functions

116
00:06:25,250 --> 00:06:28,730
that you should keep in mind, because it can come in handy

117
00:06:28,730 --> 00:06:31,220
when you are defining your own functions

118
00:06:31,220 --> 00:06:33,313
that should be a bit more flexible.

