But in Java (and similar), you have to be explicit about varargs. In JavaScript, every function supports varargs. This will run just fine:
function print(arg) {
console.log(arg);
}
print("a", "b", "c");
It’ll only output "a", obviously. In fact, this will run just fine too: print();
You’ll get "undefined" in the console, but it’ll work.JavaScript’s nature is that arguments are in the "arguments" pseudo-array, and the parameters are just fancy names for different values in "arguments". See:
function count() {
console.log(arguments.length);
}
count();
count("a");
count("a", "b");
In order, you’ll get: 0, 1, and 2. Despite the fact that "count" has no parameters in the definition.In the first function ("print"), "arg" is just syntax sugar for "arguments[0]".
What I’m getting at is: in C, Java, etc., the compiler knows what is a varargs function and what isn’t. In JavaScript, the interpreter/compiler doesn’t and has to assume everything is.