| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Functions/rest_parameters | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2016 ..
.
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
function(a, b, ...theArgs) {
// ...
}
..., 0 theArgs.length-1 , .
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" :
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- , .
function myFun(a, b, ...manyMoreArgs) {
console.log("a", a);
console.log("b", b);
console.log("manyMoreArgs", manyMoreArgs);
}
myFun("", "", "", "", "", "");
// a,
// b,
// manyMoreArgs, [, , , ]
... , .
// ,
myFun("", "", "");
// a,
// b,
// manyMoreArgs, []
... , "manyMoreArgs" ( ).
// ,
myFun("", "");
// a,
// b,
// manyMoreArgs, []
theArgs , length:
function fun1(...theArgs) {
console.log(theArgs.length);
}
fun1(); // 0
fun1(5); // 1
fun1(5, 6, 7); // 3
, . :
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:
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, .
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 |
This page was last modified on 24 . 2025 . by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |