I am new to the world of Javascript and am tinkering with writing very basic functions and stumbled upon the example below by accident and am unsure why it works when I am n
JavaScript doesn't have default values for function parameters like other languages do. So, you can pass as many or as little arguments as you want.
If you don't pass a value, the parameter is undefined
.
function myfunction(x) {
alert(x);
}
myfunction(); // alerts undefined
myfunction(1); // alerts 1
myfunction(1,2,3); // alerts 1
If you pass more parameters than are in the signature, you can use arguments.
function myfunction(x) {
alert(x);
console.log(arguments);
}
myfunction(1,2,3); // alerts 1, logs [1,2,3]