Function Parameters

Function Parameters

JavaScript allows you to pass in parameters when calling a function.

Using function Parameter

Following example, has function Hello which accepts a single parameter named yourname. The function then prints out the word "Hello" followed by the value of parameter yourname. After the function ends we initialize a variable named name with value "John Doe". Then Hello function is called and name variable is passed to the function. The Hello function will then print Hello John Doe to the document.

function Hello(yourname) {
    document.write("Hello " + yourname);
} 
let name = "John Doe";
Hello(name);

The above example will have the below output:

Hello John Doe

Passing Multiple Values

To pass more than one parameter, you will just need to separate each parameter by a comma (,).

To the previous example, I have added additional parameters to show how multiple parameters work. Now it will pass age along with name to Hello function. The Hello function will now display Hello John Doe. Your entered age is 20 years.

function Hello(yourname, yourage) {
    document.write("Hello " + yourname + ". Your entered age is " + yourage + " years." );
} 
let name = "John Doe";
let age = 20;
Hello(name, age);

The above example will have the below output:

Hello John Doe. Your entered age is 20 years.

Default Parameter Values

By default, if a value is not passed for a parameter, then its value by default is set to undefined. You can change the default value of a parameter if you wish. To set default value, you can assign a value to the parameter.

In below example, I have set the value of yourname variable to World. No value is passed when calling Hello function, so now the default value is used. Hello World is the result of below code.

function Hello(yourname="World") {
    document.write("Hello " + yourname );
} 
let name = "John Doe";
Hello();

The above example will have the below output:

Hello World

Take Care of Parameter Order

When calling the function, you will need to make sure order of parameters is same as what the function is expecting. If you forget to supply all parameters, the function will assume you have supplied the parameters from left to right, and any left over will be undefined.

To the previous example I have modified the function call Hello(name, age) to Hello(age). With this, the function will assume you have supplied the value for the first parameter, and second parameter will be undefined.

The Hello function will now display Hello 20. Your entered age is undefined years. This is because I only supplied the age as the parameter, and since it fills the parameters from left to right, yourname paramter in the function was assigned the value 20 and yourage paramter was left undefined.

function Hello(yourname, yourage) {
    document.write("Hello " + yourname + ". Your entered age is " + yourage + " years." );
} 
let name = "John Doe";
let age = 20;
Hello (age);

The above example will have the below output:

Hello 20. Your entered age is undefined years.

You can also put undefined, if you want to skip a parameter.