DRAFT
Observed
Functions must be called with the same number of arguments as defined in their signature
Functions can be called with a different number of arguments as defined in their signature
In JavaScript it is possible to call functions with any number of arguments, even if the number of arguments is different from the number of parameters accepted by the definitions of the function.
function foo(a, b) {
console.log(a + " " + b);
}
foo(); // undefined undefined
foo(1); // 1 undefined
foo(1, 2); // 1 2
foo(1, 2, 3); // 1 2
foo(1, 2, 3, 4); // 1 2
Starting from ECMAScript 9, the rest parameter syntax allows to represent an indefinite number of arguments as an array:
function foo(a, b, ...myArgs) {
console.log(`a:${a} b:${b} myArgs:[${myArgs}]`);
}
foo(); // a:undefined b:undefined myArgs:[]
foo(1); // a:1 b:undefined myArgs:[]
foo(1, 2); // a:1 b:2 myArgs:[]
foo(1, 2, 3); // a:1 b:2 myArgs:[3]
foo(1, 2, 3, 4); // a:1 b:2 myArgs:[3,4]
Before the rest parameter syntax, the aeguments
object could be used to access the anonymous arguments passed in the invocation of a function:
function foo() {
console.log(arguments[0] + " " + arguments[1]);
}
foo("Hello"); // Hello undefined
foo("Hello", "World"); // Hello World
Starting from ECMAScript 9, it is advised to use the rest parameter syntax instead of the arguments
object.