What happens if I don't pass a parameter in a Javascript function?

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 not passing a parameter when the function demands it.

Sample function

function myfunction(x) {
    alert("This is a sample alert");
}

Now if I call the function myfunction(); I am presented with the alert. Why is that that I am able to call the function without any errors or warnings when I have not passed a parameter?

EDIT

I did not expect so many great answers and I am by no means in a position yet able to say which answer is the best so am I able to request people to suggest the best answer and I'll award the acceptance to that person.


Solution 1:

Nothing will happen- meaning you won't get an error or a warning as passing the parameters in javascript is optional.
All the parameters that weren't "supplied" will have the undefined value.

function foo(x, y, z){
    //...
}

foo(1);

Inside the foo function now:

function foo(x, y, z){
    x === 1
    y === undefined
    z === undefined
}

You can even pass more arguments, like:

foo(1,2,3,4,5,7); // Valid!

You can know the amounts of parameters supplied by arguments.length from inside the function.

function foo(x, y, z) {
    console.log('x value: ' + x);
    console.log('y value: ' + y);
    console.log('z value: ' + z);
    console.log('Arguments length: ' + arguments.length);
}
console.log('Zero parameters');
foo();
console.log('Four parameters');
foo(1, 2, 3, 4);

Example of useful function that handle any amount of parameters:

function max() {
    var maxValue = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
        if (maxValue < arguments[i]) {
            maxValue = arguments[i];
        }
    }
    return maxValue;
}

alert(max(1, 5, 7, 2, 88, 32, 44));

Solution 2:

All arguments in JavaScript functions are optional (read "loosely typed").

JavaScript functions can be invoked with any number of arguments, regardless of the number of arguments named in the function definition. Because a function is loosely typed, there is no way for it to declare the type of arguments it expects, and it is legal to pass values of any type to any function. When a function is invoked with fewer arguments than are declared, the additional arguments have the undefined value.

You can refer to a function's arguments within the function by using the named argument variables or the arguments object. This object contains an entry for each argument passed to the function, the first entry's index starting at 0. For example, if a function is passed three arguments, you can refer to the argument as follows:

arguments[0]
arguments[1]
arguments[2]
  • JavaScript - The Definitive Guide, 5th Edition

Solution 3:

That's just how JavaScript works. Parameters are optional, and will have the not-really-a-value value "undefined" in the function if they're missing from a function call.

By "optional" I mean just that: invoking any function involves an arbitrarily long list of parameters. There need be no relationship between the number of parameters passed to a function and the number declared. Best way to think of this declaration, then:

function x(a, b, c) {
  // ...
}

is that you're declaring a function and binding the name "a" to the first parameter, "b" to the second, and "c" to the third. It's by no means guaranteed, however, that any of those will actually be bound to a value in any given invocation of the function later.

By the same token, you can define a function without any parameters at all, and then "find" them via the arguments object:

function noArgs() {
  var a = arguments[0], b = arguments[1], c = arguments[2];
  // ...
}

So that's not quite the same as the first function, but it's close in most ways that count practically.

The "undefined" value in JavaScript is a value, but it's semantics are kind-of unusual as languages go. In particular it's not exactly the same as the null value. Also, "undefined" itself is not a keyword; it's just a semi-special variable name!

Solution 4:

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]