|
| 1 | +### **What Are Rest and Spread Operators in JavaScript ?** |
| 2 | + |
| 3 | +The **rest** and **spread** operators, both denoted by `...`, are powerful features introduced in ES6. Despite their similar syntax, they serve different purposes depending on how and where they are used. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +### **1. The Rest Operator** |
| 8 | + |
| 9 | +The **rest operator** collects multiple elements or properties into a single variable. It is commonly used in functions, arrays, and objects. |
| 10 | + |
| 11 | +#### **Usage in Functions:** |
| 12 | + |
| 13 | +The rest operator is used to gather all remaining arguments into an array. |
| 14 | + |
| 15 | +```javascript |
| 16 | +function sum(...numbers) { |
| 17 | + return numbers.reduce((total, num) => total + num, 0); |
| 18 | +} |
| 19 | +console.log(sum(1, 2, 3, 4)); // Output: 10 |
| 20 | +``` |
| 21 | + |
| 22 | +#### **Usage in Arrays:** |
| 23 | + |
| 24 | +It collects the remaining elements of an array into a new array. |
| 25 | + |
| 26 | +```javascript |
| 27 | +const [a, b, ...rest] = [1, 2, 3, 4, 5]; |
| 28 | +console.log(a, b); // Output: 1 2 |
| 29 | +console.log(rest); // Output: [3, 4, 5] |
| 30 | +``` |
| 31 | + |
| 32 | +#### **Usage in Objects:** |
| 33 | + |
| 34 | +The rest operator collects remaining properties into a new object. |
| 35 | + |
| 36 | +```javascript |
| 37 | +const person = { name: "Alice", age: 25, country: "Wonderland" }; |
| 38 | +const { name, ...details } = person; |
| 39 | +console.log(name); // Output: Alice |
| 40 | +console.log(details); // Output: { age: 25, country: 'Wonderland' } |
| 41 | +``` |
| 42 | + |
| 43 | +--- |
| 44 | + |
| 45 | +### **2. The Spread Operator** |
| 46 | + |
| 47 | +The **spread operator** expands an array or object into individual elements or properties. It is particularly useful for copying and merging arrays or objects. |
| 48 | + |
| 49 | +#### **Usage in Arrays:** |
| 50 | + |
| 51 | +**1. Expanding Arrays:** |
| 52 | + |
| 53 | +```javascript |
| 54 | +const arr = [1, 2, 3]; |
| 55 | +console.log(...arr); // Output: 1 2 3 |
| 56 | +``` |
| 57 | + |
| 58 | +**2. Combining Arrays:** |
| 59 | + |
| 60 | +```javascript |
| 61 | +const arr1 = [1, 2]; |
| 62 | +const arr2 = [3, 4]; |
| 63 | +const combined = [...arr1, ...arr2]; |
| 64 | +console.log(combined); // Output: [1, 2, 3, 4] |
| 65 | +``` |
| 66 | + |
| 67 | +**3. Copying Arrays:** |
| 68 | + |
| 69 | +```javascript |
| 70 | +const original = [1, 2, 3]; |
| 71 | +const copy = [...original]; |
| 72 | +console.log(copy); // Output: [1, 2, 3] |
| 73 | +``` |
| 74 | + |
| 75 | +#### **Usage in Objects:** |
| 76 | + |
| 77 | +**1. Expanding Objects:** |
| 78 | + |
| 79 | +```javascript |
| 80 | +const person = { name: "Alice", age: 25 }; |
| 81 | +const clone = { ...person }; |
| 82 | +console.log(clone); // Output: { name: 'Alice', age: 25 } |
| 83 | +``` |
| 84 | + |
| 85 | +**2. Merging Objects:** |
| 86 | + |
| 87 | +```javascript |
| 88 | +const obj1 = { a: 1, b: 2 }; |
| 89 | +const obj2 = { b: 3, c: 4 }; |
| 90 | +const merged = { ...obj1, ...obj2 }; |
| 91 | +console.log(merged); // Output: { a: 1, b: 3, c: 4 } |
| 92 | +``` |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +### **3. Key Differences Between Rest and Spread** |
| 97 | + |
| 98 | +| **Aspect** | **Rest Operator** | **Spread Operator** | |
| 99 | +| ----------------- | ---------------------------------------- | --------------------------------------- | |
| 100 | +| **Purpose** | Collects multiple elements into one | Expands elements into individual pieces | |
| 101 | +| **Usage Context** | Function parameters, array destructuring | Arrays, objects, and function arguments | |
| 102 | +| **Syntax** | Appears on the left side of `=` | Appears on the right side of `=` | |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | +### **4. Examples** |
| 107 | + |
| 108 | +#### **Rest with Functions:** |
| 109 | + |
| 110 | +```javascript |
| 111 | +function greet(greeting, ...names) { |
| 112 | + return `${greeting}, ${names.join(" and ")}`; |
| 113 | +} |
| 114 | +console.log(greet("Hello", "Alice", "Bob")); // Output: Hello, Alice and Bob |
| 115 | +``` |
| 116 | + |
| 117 | +#### **Spread with Function Calls:** |
| 118 | + |
| 119 | +```javascript |
| 120 | +const numbers = [1, 2, 3]; |
| 121 | +console.log(Math.max(...numbers)); // Output: 3 |
| 122 | +``` |
| 123 | + |
| 124 | +#### **Combining Both:** |
| 125 | + |
| 126 | +```javascript |
| 127 | +function add(a, b, ...rest) { |
| 128 | + const sumRest = rest.reduce((total, num) => total + num, 0); |
| 129 | + return a + b + sumRest; |
| 130 | +} |
| 131 | +console.log(add(1, 2, 3, 4)); // Output: 10 |
| 132 | +``` |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +### **5. Use Cases** |
| 137 | + |
| 138 | +- **Rest Operator:** |
| 139 | + |
| 140 | + - Handling variable-length arguments in functions. |
| 141 | + - Collecting remaining properties during destructuring. |
| 142 | + |
| 143 | +- **Spread Operator:** |
| 144 | + - Cloning and merging arrays or objects. |
| 145 | + - Passing array elements as arguments to functions. |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +### **Conclusion** |
| 150 | + |
| 151 | +The **rest** and **spread** operators enhance the flexibility and readability of JavaScript code. While the rest operator is great for gathering values, the spread operator is ideal for spreading or copying them. Together, they simplify many common coding tasks. |
0 commit comments