[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Functions/rest_parameters [Back]  [Original]

- JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2016 9.

. JavaScript .

In this article

function sum(...theArgs) {
  let total = 0;
  for (const arg of theArgs) {
    total += arg;
  }
  return total;
}

console.log(sum(1, 2, 3));
// Expected output: 6

console.log(sum(1, 2, 3, 4));
// Expected output: 10

js
function f(a, b, ...theArgs) {
  // ...
}

"..."( U+002E FULL STOP ) ( ) JavaScript . .

js
function myFun(a, b, ...manyMoreArgs) {
  console.log("a", a);
  console.log("b", b);
  console.log("manyMoreArgs", manyMoreArgs);
}

myFun("one", "two", "three", "four", "five", "six");

//  :
// a, one
// b, two
// manyMoreArgs, [three, four, five, six]

... .

js
foo(...one, ...wrong, ...wrong);

.

js
foo(...wrong, arg2, arg3);
js
foo(arg1, arg2, ...correct);

arguments

arguments .

  • arguments . Array sort, map, forEach, pop .
  • arguments callee .
  • ...restParam ...restParam . arguments , ...restParam .

.

js
//    "arguments"    
function f(a, b) {
  let normalArray = Array.prototype.slice.call(arguments);
  // --  --
  let normalArray = [].slice.call(arguments);
  // --  --
  let normalArray = Array.from(arguments);

  let first = normalArray.shift(); // ,    
  let first = arguments.shift(); // , arguments   
}

//         

function f(...args) {
  let normalArray = args;
  let first = normalArray.shift(); // ,    
}

, a, b .

manyMoreArgs , , , , ... n , .

js
function myFun(a, b, ...manyMoreArgs) {
  console.log("a", a);
  console.log("b", b);
  console.log("manyMoreArgs", manyMoreArgs);
}

myFun("one", "two", "three", "four", "five", "six");

// a, "one"
// b, "two"
// manyMoreArgs, ["three", "four", "five", "six"] <--  

.

js
//     

myFun("one", "two", "three");

// a, "one"
// b, "two"
// manyMoreArgs, ["three"] <--    

, manyMoreArgs () .

js
//     

myFun("one", "two");

// a, "one"
// b, "two"
// manyMoreArgs, [] <--  

theArgs , length .

js
function fun1(...theArgs) {
  console.log(theArgs.length);
}

fun1(); // 0
fun1(5); // 1
fun1(5, 6, 7); // 3

, .

js
function multiply(multiplier, ...theArgs) {
  return theArgs.map((element) => {
    return multiplier * element;
  });
}

let arr = multiply(2, 15, 25, 42);
console.log(arr); // [30, 50, 84]

, arguments

Array , arguments .

js
function sortRestArgs(...theArgs) {
  let sortedArgs = theArgs.sort();
  return sortedArgs;
}

console.log(sortRestArgs(5, 3, 7, 1)); // 1, 3, 5, 7

function sortArguments() {
  let sortedArgs = arguments.sort();
  return sortedArgs;
}

console.log(sortArguments(5, 3, 7, 1));
// TypeError  (arguments.sort is not a function)

arguments Array arguments .

js
function sortArguments() {
  let args = Array.from(arguments);
  let sortedArgs = args.sort();
  return sortedArgs;
}
console.log(sortArguments(5, 3, 7, 1)); // 1, 3, 5, 7

Specification
ECMAScript 2027 LanguageSpecification
# sec-function-definitions


Web Proxy Viewer  |  New URL  |  Original Page