본문으로 건너뛰기

Spread Operator

Spread Operator

The spread operator is denoted by three dots ....

  • The spread operator is used to 'spread' out elements of an array or object.

  • It is commonly used to combine or copy arrays or objects.


Examples of Using the Spread Operator

Arrays

Combining Arrays
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const combined = [...arr1, ...arr2]; // [1, 2, 3, 4, 5, 6]
Copying an Array
let original = [1, 2, 3];
let copied = [...original]; // [1, 2, 3]

Objects

Combining Objects
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 };
const combined = { ...obj1, ...obj2 }; // { a: 1, b: 3, c: 4 }
Copying an Object
let obj1 = { a: 1, b: 2 };
let copiedObj = { ...obj1 }; // {a: 1, b: 2}