ES6+ features
πΉ 1. Spread Operator (β¦)
Section titled βπΉ 1. Spread Operator (β¦)β- π Expands elements of an array or object into another array/object.
// Arrays const arr1 = [1,2,3] const arr2 = [...arr1, 4,6] console.log(arr2) // [1,2,3,4,6]
// Object const obj = {name: 'Sonam'} const newObj = {...obj, role: 'Developer'} console.log(newObj) // {name: 'Sonam', role: 'Developer'}- β Use case: Cloning, merging, passing multiple values quickly.
πΉ 2. Rest Operator (β¦)
Section titled βπΉ 2. Rest Operator (β¦)β- π Collects remaining items into an array/object. (Opposite of spread.)
// functions function sum(...nums){ return nums.reduce((acc, curr)=> {acc+curr}, 0) }
// Destructing const [a,b, ...rest] = [10,20,30,40] console.log(a,b) // 10, 20 console.log(rest) // [30,40]-β Use case: Flexible function arguments, grouping leftovers.
πΉ 3. Destructuring
Section titled βπΉ 3. Destructuringβ- π Extracts values from arrays or objects into variables.
// Arrays const colors = ['red', 'blue', 'black'] const [first, , third] = colors; // red
// Object const person = {name: 'Test', role: 'Tester'} const {name, role} = person console.log(name, role) // Test, Tester- β Use case: Cleaner code, avoid repetitive property access.
πΉ Summary
Section titled βπΉ Summaryβ- Spread (β¦) β expands values.
- Rest (β¦) β collects values.
- Destructuring β extracts values.