코드노트

js 데이터 알아보기 / 전개 연산자 본문

Code note/자바스크립트

js 데이터 알아보기 / 전개 연산자

코드노트 2022. 6. 1. 00:04

전개 연산자 Spread

const fruits = ['Apple', 'Banana', 'Cherry', 'orange'];
console.log(fruits); // (4) ['Apple', 'Banana', 'Cherry', 'orange']
console.log(...fruits); // Apple Banana Cherry orange

- 전개연산자 ...을 사용하면 문자데이터로 출력이 가능하다.

 

 

const fruits = ['Apple', 'Banana', 'Cherry', 'orange'];
function toObject(a, b, ...c) {
  return {
    a: a,
    b: b,
    c: c
  }
};
console.log(toObject(...fruits));
// {a: 'Apple', b: 'Banana', c: Array(2)}
a: "Apple"
b: "Banana"
c: (2) ['Cherry', 'orange']

- 매개변수에서도 ...을 사용할 수 있다. 나머지 매개변수

- c에는 배열데이터로 나머지들이 들어간걸 볼 수 있다.