ES6 : Rest Operator

Rest Operator

Rest Operator ... converts the rest parameters to JavaScript array.


  1. Without rest operator
function printNums(num1, num2) {
  console.log(num1, num2);
}

printNums(1, 2, 3, 4, 5);
1 2

  1. arguments is the object of all the parameters.
function printNums(num1, num2) {
  console.log(arguments);
}

printNums(1, 2, 3, 4, 5);
[Arguments] { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 }

  1. ... is combine the rest parameters as an object.
function printNums(num1, ...num2) {
  console.log(num1, num2);
}

printNums(1, 2, 3, 4, 5);
1 [ 2, 3, 4, 5 ]

좋은 웹페이지 즐겨찾기