My Docs
Search…
Spread and Rest
1
// Rest Parameters
2
// Take in any remaining parameters passed into a function
3
// and places them in an array
4
​
5
// JavaScript will NOT tell us if we provided the wrong
6
// number of arguments to a function
7
// Used if we want to capture all arguments being passed to a function
8
// The rest parameter must be the last argument in a function definition.
9
// We can use the rest parameter to grab all remaining arguments.
10
​
11
​
12
​
13
​
14
function smoothie(ingredient1, ingredient2) {
15
console.log('My smoothie has ' + ingredient1 + ' and ' + ingredient2);
16
}
17
​
18
// smoothie('mango', 'apple');
19
​
20
​
21
​
22
​
23
​
24
// We can use rest parameters to make the function more dynamic and
25
// take in any number of arguments.
26
​
27
function restSmoothie(ingredient1, ...otherIngredients) {
28
let string = 'My smoothie has ' + ingredient1;
29
console.log('\n --- Displaying Rest Parameters: ---')
30
console.log(otherIngredients);
31
console.log(' --- End Display --- \n')
32
​
33
otherIngredients.forEach(function (ingredient) {
34
string = string + ' and ' + ingredient;
35
})
36
​
37
console.log(string);
38
}
39
​
40
// restSmoothie('mango', 'apple', 'turmeric', 'almond milk');
41
​
42
​
43
​
44
​
45
​
46
​
47
​
48
// ----------------------------------------------------
49
​
50
// Spread Operator
51
// Will spread out elements into the appropriate data structures
52
// Will spread iterable data types within function arguments
53
​
54
​
55
​
56
​
57
​
58
// Spread with Arrays:
59
​
60
let smallDogs = ['chihuahua', 'pomeranian', 'maltese'];
61
let mediumDogs = ['poodle', 'collie', 'basset hound'];
62
let largeDogs = ['saint bernard', 'great dane', 'english mastiff'];
63
​
64
// In this case,
65
// by using the spread operator, we take each element of the array
66
// and pass it separately to the new array.
67
let allDogs = [...smallDogs, ...mediumDogs, ...largeDogs];
68
​
69
// NOTE:
70
// The above is the same as:
71
// let allDogs = ['chihuahua', 'pomeranian', 'maltese', 'poodle', 'collie', 'basset hound','saint bernard', 'great dane', 'mastiff'];
72
​
73
// console.log(allDogs);
74
​
75
​
76
​
77
​
78
​
79
​
80
​
81
// Spread with Objects
82
​
83
let onlineInstructors = {
84
g: 'Gordon',
85
s: 'Soon-Mi',
86
a: 'Angela',
87
j: 'Justin'
88
};
89
​
90
let nyInstructors = {
91
d: 'David',
92
p: 'Paloma',
93
k: 'Kafele',
94
j: 'Josh'
95
};
96
​
97
let combinedInstructors = { ...nyInstructors };
98
combinedInstructors.m = 'Manny';
99
​
100
console.log(combinedInstructors);
101
console.log(nyInstructors);
102
​
103
​
104
​
105
​
106
​
107
​
108
​
109
​
110
​
111
​
112
// Spread when passing arguments to a function
113
​
114
// The individual values from the array / object are passed
115
// as arguments to the function
116
​
117
function goodDogs (dog1, dog2, dog3) {
118
console.log('A ' + dog1 + ' is a happy dog!');
119
console.log('A ' + dog2 + ' is a playful dog!');
120
console.log('A ' + dog3 + ' is a sleepy dog!');
121
}
122
​
123
// console.log(smallDogs);
124
// goodDogs(smallDogs);
125
​
126
// console.log(mediumDogs);
127
// goodDogs(mediumDogs);
128
​
129
// console.log(largeDogs);
130
// goodDogs(largeDogs);
Copied!
Last modified 2mo ago
Copy link