Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can I declare optional function parameters in JavaScript?
This tutorial will teach us how to declare optional function parameters in JavaScript. While declaring the function, we pass some variables to the function definition to use it inside the function block, called function parameters. The function parameters can also be optional, which gives us the independence to pass function arguments when we call the function. Here, Arguments are the values we pass while calling the function, and parameters are the variables we pass in the function definition.
What Are Optional Parameters?
The optional parameter word means that you don't need to pass that parameter every time you call the function, giving you the independence to pass fewer arguments to the function.
Simply, we can say that if we don't pass the parameters, we still don't get any parameters error while executing the function, which is called optional parameters.
You can make all the parameters optional in JavaScript by not passing any argument to the function call.
There are different approaches to handling optional function parameters in JavaScript:
- Using default parameter values (ES6+)
- Using the arguments.length property
- Using the logical OR operator ('||')
Using Default Parameter Values (ES6+)
In this approach, we assign default values to the parameters directly in the function definition. If an argument is not provided for that parameter, JavaScript automatically uses the default value.
Syntax
function functionName(parameter1, parameter2 = defaultValue, parameter3 = defaultValue) {
// parameter2 and parameter3 are optional parameters
}
Parameters
defaultValue ? It can be any value of string, number, boolean, object, or other data types.
Example
In the below example, we demonstrate how to use default parameter values to create optional parameters.
<!DOCTYPE html>
<html>
<body>
<h2>Declaring optional function parameters by passing Default values.</h2>
<h4>Result when calling function with all parameters:</h4>
<p id="resultDiv1"></p>
<h4>Result when calling function with only required parameters:</h4>
<p id="resultDiv2"></p>
<script>
function calculateSum(a, b = 10, c = 5) {
return a + b + c;
}
// Call with all parameters
document.getElementById("resultDiv1").innerHTML =
"calculateSum(1, 2, 3) = " + calculateSum(1, 2, 3);
// Call with only required parameter (b and c use defaults)
document.getElementById("resultDiv2").innerHTML =
"calculateSum(1) = " + calculateSum(1);
</script>
</body>
</html>
calculateSum(1, 2, 3) = 6 calculateSum(1) = 16
Using the arguments.length Property
In JavaScript, every function has an arguments object that contains all passed parameters. We can use arguments.length to determine how many parameters were passed and assign default values accordingly.
Syntax
function functionName(parameter1, parameter2, parameter3) {
if(arguments.length === 0) {
// assign values to all parameters
} else if(arguments.length === 1) {
// assign values to last 2 parameters
} else if(arguments.length === 2) {
// assign value to last parameter
}
}
Example
This example shows how to use arguments.length to handle optional parameters.
<!DOCTYPE html>
<html>
<body>
<h2>Using arguments.length property for optional parameters</h2>
<p>With 2 parameters: <span id="result1"></span></p>
<p>With 1 parameter: <span id="result2"></span></p>
<p>With no parameters: <span id="result3"></span></p>
<script>
function multiply(num1, num2) {
if (arguments.length === 0) {
num1 = 2;
num2 = 5;
} else if (arguments.length === 1) {
num2 = 3; // default value for second parameter
}
return num1 * num2;
}
document.getElementById("result1").innerHTML = multiply(4, 6);
document.getElementById("result2").innerHTML = multiply(7);
document.getElementById("result3").innerHTML = multiply();
</script>
</body>
</html>
With 2 parameters: 24 With 1 parameter: 21 With no parameters: 10
Using the Logical OR Operator ('||')
The logical OR operator (||) can be used to assign default values. If the parameter is undefined (not passed), the OR operator assigns the default value on the right side.
Syntax
function functionName(parameter1, parameter2, parameter3) {
let var1 = parameter1 || defaultValue;
let var2 = parameter2 || defaultValue;
}
Example
This example demonstrates using the logical OR operator for optional parameters.
<!DOCTYPE html>
<html>
<body>
<h2>Using logical OR operator for optional parameters</h2>
<p>No parameters: <span id="contentDiv1"></span></p>
<p>One parameter: <span id="contentDiv2"></span></p>
<p>Both parameters: <span id="contentDiv3"></span></p>
<script>
function greetUser(name, greeting) {
let userName = name || "Guest";
let userGreeting = greeting || "Hello";
return userGreeting + ", " + userName + "!";
}
document.getElementById("contentDiv1").innerHTML = greetUser();
document.getElementById("contentDiv2").innerHTML = greetUser("Alice");
document.getElementById("contentDiv3").innerHTML = greetUser("Bob", "Hi");
</script>
</body>
</html>
No parameters: Hello, Guest! One parameter: Hello, Alice! Both parameters: Hi, Bob!
Comparison of Methods
| Method | ES6 Compatible | Readability | Best Use Case |
|---|---|---|---|
| Default Parameters | Yes | Excellent | Modern applications |
| arguments.length | No (legacy) | Good | Complex conditional logic |
| Logical OR (||) | No (legacy) | Good | Simple default assignments |
Conclusion
Default parameter values (ES6+) are the recommended modern approach for optional function parameters. Use arguments.length or logical OR operator for legacy browser support or complex scenarios.
