| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/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 9.
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 f(a, b, ...theArgs) {
// ...
}
"..."( U+002E FULL STOP ) ( ) JavaScript . .
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]
... .
foo(...one, ...wrong, ...wrong);
.
foo(...wrong, arg2, arg3);
foo(arg1, arg2, ...correct);
arguments arguments . Array sort, map, forEach, pop .arguments callee ....restParam ...restParam . arguments , ...restParam ..
// "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 , .
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"] <--
.
//
myFun("one", "two", "three");
// a, "one"
// b, "two"
// manyMoreArgs, ["three"] <--
, manyMoreArgs () .
//
myFun("one", "two");
// a, "one"
// b, "two"
// 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((element) => {
return multiplier * element;
});
}
let arr = multiply(2, 15, 25, 42);
console.log(arr); // [30, 50, 84]
arguments Array , arguments .
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 .
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 |
This page was last modified on 2025 6 27 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 |