1
00:00:01,349 --> 00:00:03,683
Default parameters allow you to give a

2
00:00:03,683 --> 00:00:06,849
default value to a parameter in a

3
00:00:06,849 --> 00:00:09,949
function. Here's an example. We have

4
00:00:09,949 --> 00:00:12,049
function sayHi that takes in one parameter

5
00:00:12,049 --> 00:00:15,778
name and contains a single line of

6
00:00:15,778 --> 00:00:17,849
statement. This is what is called a

7
00:00:17,849 --> 00:00:20,749
default parameter. We have two ways we can

8
00:00:20,749 --> 00:00:23,349
invoke the function sayHi. First, we

9
00:00:23,349 --> 00:00:26,849
invoke it without any arguments, as shown

10
00:00:26,849 --> 00:00:28,849
here. The name variable will be

11
00:00:28,849 --> 00:00:30,349
initialized with the value 'World',

12
00:00:30,349 --> 00:00:34,683
printing Hello World to the console. Next,

13
00:00:34,683 --> 00:00:37,149
we invoke the function sayHi with an

14
00:00:37,149 --> 00:00:39,549
argument like John. This time, since we

15
00:00:39,549 --> 00:00:42,149
have provided a value for name, the string

16
00:00:42,149 --> 00:00:45,349
Hello John will be printed to the console.

17
00:00:45,349 --> 00:00:48,349
Let's take a look at another example.

18
00:00:48,349 --> 00:00:50,974
Here, we have the same function as

19
00:00:50,974 --> 00:00:52,778
earlier, but it takes an additional

20
00:00:52,778 --> 00:00:55,599
parameter message without any default

21
00:00:55,599 --> 00:00:59,349
value. First, we invoke the function sayHi

22
00:00:59,349 --> 00:01:02,016
with only the argument 'Hello'. In this

23
00:01:02,016 --> 00:01:04,349
case, the name parameter value is 'World',

24
00:01:04,349 --> 00:01:07,849
and Hello World is displayed to the

25
00:01:07,849 --> 00:01:09,778
console. Then, we invoke the function

26
00:01:09,778 --> 00:01:13,683
sayHi again with two arguments: 'Hi' and

27
00:01:13,683 --> 00:01:18,683
'John'. Now the output is Hi John. Please

28
00:01:18,683 --> 00:01:21,349
note that the default parameter in our

29
00:01:21,349 --> 00:01:23,749
function definition should always come

30
00:01:23,749 --> 00:01:26,849
after the regular parameters. So in our

31
00:01:26,849 --> 00:01:30,492
sayHi function here, message cannot come

32
00:01:30,492 --> 00:01:34,474
after the name parameter. Let's see this

33
00:01:34,474 --> 00:01:37,849
in action in our demo. Here's the first

34
00:01:37,849 --> 00:01:40,749
example. If you save the file, we get the

35
00:01:40,749 --> 00:01:44,849
expected output in the console. Here's the

36
00:01:44,849 --> 00:01:47,749
second example with the additional message

37
00:01:47,749 --> 00:01:51,349
parameter. Again, we save and get the

38
00:01:51,349 --> 00:01:54,749
expected output printed to the console. If

39
00:01:54,749 --> 00:01:57,549
we move the message parameter after the

40
00:01:57,549 --> 00:02:00,016
name parameter, we get undefined in the

41
00:02:00,016 --> 00:02:03,549
console, and message never gets

42
00:02:03,549 --> 00:02:06,349
initialized. Therefore, always declare the

43
00:02:06,349 --> 00:02:11,000
default parameter after the regular parameters in your function definition.

