전개 연산자(Spread syntax)는 반복 가능한 객체에 적용할 수 있는 문법이다. 배열이나 문자열 등을 풀어서 요소 하나 하나로 전개시킬 수 있다.
const arr = [1, 2, 3, 4, 5];
const str = "string";
console.log(...arr); // 1 2 3 4 5
console.log(...str); // "s" "t" "r" "i" "n" "g"
전개 연산자는 배열에 요소를 추가
하거나 배열을 합치는 경우
에도 유용하게 사용된다.
// 배열에 요소를 추가하는 경우
const arr1 = [1, 2, 3]
console.log(['a', 'b', ...arr1, 'c']) // ['a', 'b', 1, 2, 3, 'c']
// 배열을 합치는 경우
const arr2 = [10, 20, 30]
const arr3 = [40, 50, 60, 70]
console.log([...arr2, ...arr3]) // [10, 20, 30, 40, 50, 60, 70]