자바스크립트 함수 다루기

iadorora·2024년 11월 6일

daily record

목록 보기
15/27
function firstWords() {
  let word = '';

  for(const arg of arguments) {
    word += arg[0];
  }

  console.log(word);
}

firstWords('느낌', '좋은', '카페'); //느좋카
firstWords('아이스', '바닐라', '라떼'); //아바라

✅ rest parameter가 arguments 객체와는 다르게 배열이라는 점을 응용

function ignoreFirst(...rest) {
  rest.shift();
  
  for (const el of rest) {
    console.log(el);
  }
}

ignoreFirst('1세대', '2세대', '3세대'); //2세대 3세대
ignoreFirst('곰팡이', '강아지', '고양이'); //강아지 고양이
ignoreFirst(20, 9, 18, 19, 30, 34, 40); //9 18 19 30 34 40 

✅ rest parameter와 일반 parameter 함께 사용

function ignoreFirst(first, ...rest) {
  for (const el of rest) {
    console.log(el);
  }
}

ignoreFirst('1세대', '2세대', '3세대'); //2세대 3세대
ignoreFirst('곰팡이', '강아지', '고양이'); //강아지 고양이
ignoreFirst(20, 9, 18, 19, 30, 34, 40); //9 18 19 30 34 40
profile
Archive

0개의 댓글