[JS] 헷갈리는 문법들 알고가자

imura·2023년 4월 30일
0

JS

목록 보기
2/3

자주 쓰는 문법들만 쓰다 보니 모르는 게 너무 많은 것 같아서 공부하고자 기록한다
계속해서 모르는 걸 찾을 때마다 추가적으로 기록할 예정

문법 정리

Rest parameters 나머지 매개변수

// rest parameters 배열
function testRestParams(a, b, ...args) {
  console.log("a", a); // a 1
  console.log("b", b); // b 2
  console.log("and more", args); // and more [3,4,5,6]
}

testRestParams(1,2,3,4,5,6); 

// arguments 유사배열
function testArguments(){
  console.log(arguments); // [Arguments] { '0': 1, '1': 2, '2': 3 }
  const arr = Array.from(arguments); // 배열로 만들어서 사용해야함
  console.log('arr', arr); // arr [1,2,3]
}

testArguments(1,2,3)

참고 사이트

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Functions/rest_parameters

0개의 댓글