function fn(...rest) {
    console.log(rest); // 配列として渡されたrest
}

fn(1, 2, 3); // 出力: [1, 2, 3]
fn(1, 2, 3, 4); // 出力: [1, 2, 3, 4]
fn(1, 2, 3, 4, 5); // 出力: [1, 2, 3, 4, 5]
const expressionFn = function (...rest) {
    console.log(rest); // 配列として渡されたrest
}

expressionFn(1, 2, 3); // 出力: [1, 2, 3]
expressionFn(1, 2, 3, 4); // 出力: [1, 2, 3, 4]
expressionFn(1, 2, 3, 4, 5); // 出力: [1, 2, 3, 4, 5]
const arrowFn = (...rest) => {
    console.log(rest); // 配列として渡されたrest
}

arrowFn(1, 2, 3); // 出力: [1, 2, 3]
arrowFn(1, 2, 3, 4); // 出力: [1, 2, 3, 4]
arrowFn(1, 2, 3, 4, 5); // 出力: [1, 2, 3, 4, 5]
/* 固定引数がある場合 */
function functionName(param1, [param2,] ...restParams) {
    // 関数本文でrestParamsを配列として使用できます。
}

/* 固定引数がない場合 */
function functionName(...restParams) {
    // 関数本文でrestParamsを配列として使用できます。
}
function myFun(a, b, ...manyMoreArgs) {
    console.log("a:", a);
    console.log("b:", b);
    console.log("残余引数 manyMoreArgs:", manyMoreArgs);
}

myFun("apple", "banana", "cherry", "date", "elderberry", "fig");

// 出力:
// "a: apple"
// "b: banana"
// "残余引数 manyMoreArgs:" [ 'cherry', 'date', 'elderberry', 'fig' ]
function wrong1(...one, ...wrong) {} // エラー
function wrong2(...wrong, arg2, arg3) {} // エラー
function exampleFunction(a, b, ...restParams) {
    // 関数本文
}

console.log(exampleFunction.length); // 出力: 2
console.log("Hello", "World", "!");
// 出力: "Hello World !"
function sum(...numbers) {
    let total = 0;
    
    for (let number of numbers) {
        total += number;
    }
    
    return total;
}

console.log(sum(1, 2, 3, 4, 5)); // 出力: 15