Parameters

By: Dusty Arlia
Published on November 14, 2012
Last Updated on Tuesday, July 14, 2015 at 12:02 PM
Total Updates: 6

Parameters are variable-like items that receive values from function calls. Parameters are an optional part of the function declaration. There can be multiple parameters or no parameters at all. Even though there is no limit on the number of parameters you can have, a typical amount is one or two parameters. Parameters are filled with values when the function is called. For example, you might create a function to calculate the checkout total of a visitor's shopping cart. This function will need to know which items were purchased and how many of each item were purchased.

When creating the function, place the parameter(s) in between the parentheses after the function name. If there is more than one parameter, use commas to separate them. Here is an example:

function shoppingCartCalculator(item_number, quantity) {
//shoppingCartCalculator JavaScript Code
}

The parameter is just like a variable, so you can give it any valid variable name. In the example above, we used the parameters item_number and quantity.

For another example, if you wanted to replace the browser's document.write() function with a shorter name, you can build your own function. Name the function print(), add a parameter called message, and put document.write(message) in the function's body. Here is how it should look:

function print(message) {
document.write(message);
}

When you call this function, you do so by using its function name, followed by the argument(s) you want to pass to the function in between the parentheses. Here is an example of how you would call this function:

print('Hello World!');

When the print() function is called, the Hello World! string is passed to the message parameter. The process of sending an argument(s) to a function is called "passing an argument". In this example, the Hello World! string is the argument.

You can include any number of parameters in your function declaration and you can pass any number of arguments to that function. Here is the syntax for a function with multiple parameters:

function functionName(parameter1, parameter2, parameter3) {
//Your JavaScript function code 
}

And this is how you call your function with an equal amount of arguments, in the same order:

functionName(argument1, argument2, argument3);

When you call this function argument1 is stored in parameter1, argument2 in parameter2, and argument3 in parameter3.

If you are passing variables as your arguments, you might notice that their names might not be the same as the parameter names. You are allowed to have parameters with the same names as the arguments, but this opens up the small chance that you can have a name collision. It is recommended that you use different names for your parameters.

The JavaScript interpreter doesn't generate any errors if the function declaration has more parameters than arguments you pass. Chances are that you will get unexpected results, because the function might be expecting one of those arguments.

Likewise, if you pass more arguments than available parameters, the JavaScript interpreter will silently ignore the extra arguments (extra arguments are still accessible from the arguments array). It's common practice that you should specify enough parameters in your function declaration to receive the amount of arguments you plan on passing. This makes your code easier to read and maintain. If your function receives an arbitrary number of arguments, pass an array as a single argument instead.

Comments:

Ad: