المعرفة:: JavaScript
الحالة::مؤرشفة
المراجع:: syahshiimi’s notes, The Complete JavaScript Course 2022 From Zero to Expert
- Spread operator allows us to unpack all elements in an array.
- We can only use it in values where it is separated by commas (arrays or objects).
- It can be used to (shallow) copy and merge arrays.
- The spread operator works with all
iterables
— things such as arrays, strings, maps or sets but not objects.
- We can use it only while building an array or passing values into a function.
Spread vs Destructuring
The spread operator is different to the destructuring of arrays as it takes all the elements in an array and it doesn’t create new variables.
Spread in Objects
Spread can be used to shallow copy objects, with or without changing their attributes.