1
00:00:01,427 --> 00:00:04,427
The spread operator is the opposite of how

2
00:00:04,427 --> 00:00:07,177
a rest parameter works. It allows the

3
00:00:07,177 --> 00:00:09,427
function to take an array as an argument

4
00:00:09,427 --> 00:00:12,316
and then spread out its elements so that

5
00:00:12,316 --> 00:00:14,982
they can be assigned to individual

6
00:00:14,982 --> 00:00:18,093
parameters. Here, we have a function greet

7
00:00:18,093 --> 00:00:23,427
with two parameters, person1 and person2.

8
00:00:23,427 --> 00:00:25,177
We invoke the function, but instead of

9
00:00:25,177 --> 00:00:28,608
passing in two arguments, we pass an array

10
00:00:28,608 --> 00:00:31,027
with the spread operator. Although it

11
00:00:31,027 --> 00:00:33,127
looks very similar to the rest parameter

12
00:00:33,127 --> 00:00:35,827
example in the previous module, we are

13
00:00:35,827 --> 00:00:38,427
doing the opposite here. We are passing an

14
00:00:38,427 --> 00:00:40,627
array as an argument and then spreading

15
00:00:40,627 --> 00:00:43,427
its elements into individual parameters of

16
00:00:43,427 --> 00:00:46,427
the function. So the two values in names

17
00:00:46,427 --> 00:00:49,855
get assigned to person1 and person2, and

18
00:00:49,855 --> 00:00:53,227
the output Hello Mary and John is

19
00:00:53,227 --> 00:00:57,552
displayed in the console. In this example,

20
00:00:57,552 --> 00:01:00,316
we define a function display with four

21
00:01:00,316 --> 00:01:03,427
parameters and print them to the console.

22
00:01:03,427 --> 00:01:06,927
Next, we declare and initialize a variable

23
00:01:06,927 --> 00:01:10,093
letters to the string 'abcd'. Finally, we

24
00:01:10,093 --> 00:01:12,284
call the display function, passing the

25
00:01:12,284 --> 00:01:15,570
string letters as an argument along with

26
00:01:15,570 --> 00:01:20,141
the spread operator "... ". The output is

27
00:01:20,141 --> 00:01:24,141
printed to the console. Since string is an

28
00:01:24,141 --> 00:01:26,552
array of characters, each of the

29
00:01:26,552 --> 00:01:28,727
characters in the variable letters is

30
00:01:28,727 --> 00:01:30,827
spread out to the parameters of the

31
00:01:30,827 --> 00:01:32,627
display function. So the spread operator

32
00:01:32,627 --> 00:01:35,627
can be used with any iterable like string,

33
00:01:35,627 --> 00:01:40,699
array, or list. Let's take a look at the

34
00:01:40,699 --> 00:01:44,093
spread operator in the demo. The display

35
00:01:44,093 --> 00:01:45,927
function accepts four parameters and

36
00:01:45,927 --> 00:01:49,316
prints them to the console. The string

37
00:01:49,316 --> 00:01:51,982
'abcd' is assigned to the variable

38
00:01:51,982 --> 00:01:54,427
letters. We run the display function,

39
00:01:54,427 --> 00:01:57,538
passing in the letters variable using the

40
00:01:57,538 --> 00:02:00,302
spread operator. So the first letter gets

41
00:02:00,302 --> 00:02:03,712
assigned to char1, the second to char2,

42
00:02:03,712 --> 00:02:07,927
and so on. When we save, we get the output

43
00:02:07,927 --> 00:02:11,760
a b c d in the console. What happens if we

44
00:02:11,760 --> 00:02:14,245
initialize letters with more than four

45
00:02:14,245 --> 00:02:17,427
letters and then run the display function?

46
00:02:17,427 --> 00:02:18,827
As we can see, the additional letters

47
00:02:18,827 --> 00:02:22,093
never get collected since we only have

48
00:02:22,093 --> 00:02:25,427
four parameters. What if we add a rest

49
00:02:25,427 --> 00:02:27,790
parameter called others to the display

50
00:02:27,790 --> 00:02:31,427
function and then print it to the console?

51
00:02:31,427 --> 00:02:33,427
As you can see, all the additional letters

52
00:02:33,427 --> 00:02:36,177
get stored in the rest parameter. So it's

53
00:02:36,177 --> 00:02:39,427
easy to use both rest parameters and the

54
00:02:39,427 --> 00:02:42,855
spread operator at the same time. To

55
00:02:42,855 --> 00:02:45,427
review, while a rest parameter collects

56
00:02:45,427 --> 00:02:48,052
individual arguments and stores them in an

57
00:02:48,052 --> 00:02:50,677
array, the spread operator takes an array

58
00:02:50,677 --> 00:02:54,760
as argument and spreads it into individual

59
00:02:54,760 --> 00:02:58,000
parameters. Let's wrap up this module with a summary.

