1
00:00:01,512 --> 00:00:04,137
With rest parameters, you can define a

2
00:00:04,137 --> 00:00:06,762
function and store multiple arguments in a

3
00:00:06,762 --> 00:00:09,112
single array. This is especially useful

4
00:00:09,112 --> 00:00:12,512
when you are invoking a function with

5
00:00:12,512 --> 00:00:16,512
multiple arguments. Let's look at it in

6
00:00:16,512 --> 00:00:19,312
more detail with an example. Here, we

7
00:00:19,312 --> 00:00:22,112
define a greet function with the names

8
00:00:22,112 --> 00:00:25,312
parameter. The "... " is what makes names

9
00:00:25,312 --> 00:00:28,912
a rest parameter and allows greet to

10
00:00:28,912 --> 00:00:32,512
accept multiple arguments as shown here.

11
00:00:32,512 --> 00:00:35,179
We pass three string values, 'Mary',

12
00:00:35,179 --> 00:00:38,762
'John', and 'James', to the greet

13
00:00:38,762 --> 00:00:41,387
function. The names parameter stores all

14
00:00:41,387 --> 00:00:45,512
of these values as an array. In the greet

15
00:00:45,512 --> 00:00:48,012
function, we call the forEach method on

16
00:00:48,012 --> 00:00:51,179
the names array. We iterate over each name

17
00:00:51,179 --> 00:00:53,941
value and print the string 'Hi ', followed

18
00:00:53,941 --> 00:00:57,369
by the name to the console. So the rest

19
00:00:57,369 --> 00:00:59,512
parameter really allows us to send any

20
00:00:59,512 --> 00:01:02,369
number of arguments to a function and then

21
00:01:02,369 --> 00:01:05,845
collect them in an array. Here's another

22
00:01:05,845 --> 00:01:08,512
example. We have the same greet function,

23
00:01:08,512 --> 00:01:11,512
but with an additional parameter message.

24
00:01:11,512 --> 00:01:14,512
Again, just like the default parameters,

25
00:01:14,512 --> 00:01:17,512
rest parameters should appear after any

26
00:01:17,512 --> 00:01:20,762
regular parameters in your function

27
00:01:20,762 --> 00:01:22,762
definition. We call the greet function

28
00:01:22,762 --> 00:01:25,512
with four arguments, 'Welcome', 'Mary',

29
00:01:25,512 --> 00:01:29,512
'John', and 'James'. The value 'Welcome'

30
00:01:29,512 --> 00:01:31,512
gets assigned to the message parameter

31
00:01:31,512 --> 00:01:33,694
while the rest of the arguments get stored

32
00:01:33,694 --> 00:01:36,912
as an array in the names parameter. This

33
00:01:36,912 --> 00:01:40,112
is also how a rest parameter gets its

34
00:01:40,112 --> 00:01:43,512
name. It always stores the rest, or

35
00:01:43,512 --> 00:01:47,083
remaining arguments, as an array. Let's

36
00:01:47,083 --> 00:01:51,262
run this example in our demo. Here's a

37
00:01:51,262 --> 00:01:53,512
greet function with the message parameter

38
00:01:53,512 --> 00:01:56,941
and the rest parameter names. We call the

39
00:01:56,941 --> 00:01:58,226
function with the values 'Welcome',

40
00:01:58,226 --> 00:02:03,083
'Mary', 'John', and 'James', and save. The

41
00:02:03,083 --> 00:02:06,712
output is printed to the console. Let's

42
00:02:06,712 --> 00:02:08,312
see what happens if we remove the message

43
00:02:08,312 --> 00:02:11,679
parameter from the function definition. We

44
00:02:11,679 --> 00:02:13,369
also remove the first statement so we

45
00:02:13,369 --> 00:02:16,239
don't get a reference error. Now, the

46
00:02:16,239 --> 00:02:18,785
Welcome string is part of the rest

47
00:02:18,785 --> 00:02:21,330
parameter names, and we get this

48
00:02:21,330 --> 00:02:24,057
unexpected line, Hi Welcome, in the

49
00:02:24,057 --> 00:02:27,083
console. This is not what we want, so even

50
00:02:27,083 --> 00:02:29,312
though the rest parameters provide a great

51
00:02:29,312 --> 00:02:31,312
deal of flexibility, you need to be

52
00:02:31,312 --> 00:02:34,083
careful when they are mixed with regular

53
00:02:34,083 --> 00:02:36,734
parameters. Let's add the message

54
00:02:36,734 --> 00:02:40,637
parameter after names and save. We get the

55
00:02:40,637 --> 00:02:43,179
error stating that rest parameter must be

56
00:02:43,179 --> 00:02:44,812
the last formal parameter. That's

57
00:02:44,812 --> 00:02:47,212
expected, as you want the rest of the

58
00:02:47,212 --> 00:02:49,512
arguments to be stored in a rest parameter

59
00:02:49,512 --> 00:02:56,000
and not a regular parameter. Next, let's take a look at the spread operator.

