What Is a Parameter in Programming?
A parameter specifies the name you wish to call your function’s argument.
A parameter is an optional component of a function. In other words, you do not need to specify a parameter if your function does not accept any argument.
For instance, JavaScript’s pop() method is a function without any parameter because it does not accept arguments.
On the other hand, forEach() has two parameters that accept two arguments.
The Syntax of a Parameter
Section titled “The Syntax of a Parameter”function nameOfFunction(parameter1, parameter2, parameter3) { // function's body}The function in the snippet above has three parameters.
Example of a JavaScript Parameter
Section titled “Example of a JavaScript Parameter”// Define a function with two parameters:function myName(firstName, lastName) { console.log(`My full name is ${firstName} ${lastName}.`);}
// Invoke myName function while passing two arguments to its parameters:myName("Oluwatobi", "Sofela");
// The invocation above will return: "My full name is Oluwatobi Sofela."The myName() function in the snippet above has two parameters: firstName and lastName.
Suppose you wish to pre-define values for your parameters that browsers can use if users do not invoke the function with the required arguments. In that case, you can create default parameters.
What Is a Default Parameter?
Section titled “What Is a Default Parameter?”Default parameters allow you to initialize your function’s parameters with default values.
For instance, suppose a user invokes your function without providing a required argument. In such a case, browsers will set the parameter’s value to undefined.
However, default parameters allow you to define the values browsers should use instead of undefined.
Examples of Default Parameters
Section titled “Examples of Default Parameters”Below are examples of how default parameters work in JavaScript.
How to define a function with no default parameters
Section titled “How to define a function with no default parameters”// Define a function with two parameters:function myName(firstName, lastName) { console.log(`My full name is ${firstName} ${lastName}.`);}
// Invoke myName function while passing one argument to its parameters:myName("Oluwatobi");
// The invocation above will return: "My full name is Oluwatobi undefined."The computer automatically set the lastName parameter to undefined because we did not provide a default value.
How to define a function with an undefined argument and no default parameter
Section titled “How to define a function with an undefined argument and no default parameter”// Define a function with two parameters:function myName(firstName, lastName) { console.log(`My full name is ${firstName} ${lastName}.`);}
// Invoke myName function while passing two arguments to its parameters:myName("Oluwatobi", undefined);
// The invocation above will return: "My full name is Oluwatobi undefined."The computer set the lastName parameter to undefined because we provided undefined as myName()’s second argument.
How to define a function with a default parameter
Section titled “How to define a function with a default parameter”// Define a function with two parameters:function myName(firstName, lastName = "Sofela") { console.log(`My full name is ${firstName} ${lastName}.`);}
// Invoke myName function while passing one argument to its parameters:myName("Oluwatobi");
// The invocation above will return: "My full name is Oluwatobi Sofela."Instead of undefined, JavaScript used "Sofela" as the lastName parameter’s default argument.
How to define a function with an undefined argument and a default parameter
Section titled “How to define a function with an undefined argument and a default parameter”// Define a function with two parameters:function myName(firstName, lastName = "Sofela") { console.log(`My full name is ${firstName} ${lastName}.`);}
// Invoke myName function while passing two arguments to its parameters:myName("Oluwatobi", undefined);
// The invocation above will return: "My full name is Oluwatobi Sofela."Instead of undefined, JavaScript used "Sofela" as the lastName parameter’s default argument.