What happens when I call a Javascript function which takes parameters, without supplying those parameters?
Set to undefined. You don't get an exception. It can be a convenient method to make your function more versatile in certain situations. Undefined evaluates to false, so you can check whether or not a value was passed in.
javascript will set any missing parameters to the value undefined
.
function fn(a) {
console.log(a);
}
fn(1); // outputs 1 on the console
fn(); // outputs undefined on the console
This works for any number of parameters.
function example(a,b,c) {
console.log(a);
console.log(b);
console.log(c);
}
example(1,2,3); //outputs 1 then 2 then 3 to the console
example(1,2); //outputs 1 then 2 then undefined to the console
example(1); //outputs 1 then undefined then undefined to the console
example(); //outputs undefined then undefined then undefined to the console
also note that the arguments
array will contain all arguments supplied, even if you supply more than are required by the function definition.