Spread Operator
The Spread Operator is composed of three dots, ...
.
-
The spread operator is used to 'spread' elements of an array or object.
-
It is mainly used for merging or copying arrays or objects.
Examples of Using the Spread Operator
Arrays
Merging Arrays
const arr1 = [1, 2, 3]; const arr2 = [4, 5, 6]; const combined = []; // [1, 2, 3, 4, 5, 6] console.log(combined);
Copying Arrays
const original = [1, 2, 3]; const copied = [...original]; console.log(copied); // [1, 2, 3]
Objects
Merging Objects
const obj1 = { a: 1, b: 2 }; const obj2 = { b: 3, c: 4 }; const combined = { ...obj1, ...obj2 }; console.log(combined); // { a: 1, b: 3, c: 4 }
Copying Objects
const obj1 = { a: 1, b: 2 }; const copiedObj = { ...obj1 }; // {a: 1, b: 2} console.log(copiedObj);
Mission
0 / 1
The spread operator is used to merge arrays and objects.
True
False
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Code Editor
Run
Execution Result