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

(rest parameters) - 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

(rest parameters)

Baseline Widely available

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

.

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(a, b, ...theArgs) {
  // ...
}

..., 0 theArgs.length-1 , .

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

myFun("", "", "", "", "", "");

// Console Output:
// a, 
// b, 
// manyMoreArgs, [, , , ]

arguments

arguments:

:

js
//     "arguments"     :

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

  var first = normalArray.shift(); // OK,   
  var first = arguments.shift(); // ERROR (arguments    )
}

//          

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

( ). , . .

function f(...[a, b, c]) {
  return a + b + c;
}

f(1)          // NaN (b  c  undefined)
f(1, 2, 3)    // 6
f(1, 2, 3, 4) // 6 (   )

"a", "b", . "manyMoreArgs" , 3-, 4-, 5-, 6- ... n- , .

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

myFun("", "", "", "", "", "");

// a, 
// b, 
// manyMoreArgs, [, , , ]

... , .

js
//    ,     

myFun("", "", "");

// a, 
// b, 
// manyMoreArgs, []

... , "manyMoreArgs" ( ).

js
//    ,     

myFun("", "");

// a, 
// b, 
// 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(function (element) {
    return multiplier * element;
  });
}

var arr = multiply(2, 1, 2, 3);
console.log(arr); // [2, 4, 6]

Array , arguments:

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

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

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

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

Array arguments, .

js
function sortArguments() {
  var args = Array.from(arguments);
  var 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