Front-End/자바스크립트
[자바스크립트] ... 연산자(Spread 연산자) 사용 예제
shoney9254
2022. 6. 19. 17:37
반응형
1. 객체에서 Spread 연산자 사용
소스 코드
const cookie = {
base: "cookie",
madeIn: "Korea"
};
const chocoCookie = {
...cookie,
toping: "choco"
};
console.log(chocoCookie);
결과

객체를 생성할 때 자주 반복되는 프로퍼티를 직접 입력하지 않고, ...객체명 을 사용해서 손쉽게 복제?할 수 있다.
2. 배열에서 Spread 연산자 사용
소스 코드
const arrA = ["가", "나"];
const arrB = ["다", "라"];
const arrC = [...arrA, ...arrB];
console.log(arrC);
결과

배열에서도 배열명 앞에 ...을 입력해서 사용할 수 있다.
반응형