JavaScript

Is There a Better Way to Do Optional Function Parameters in JavaScript?

Functions in JavaScript frequently include optional parameters that allow the caller to modify the function’s behavior. However, dealing with optional parameters can be difficult and error-prone, particularly when working with large functions with multiple parameters. While there are various alternative ways to specify optional function parameters in JavaScript that can simplify and improve the robustness of the code.

This blog will demonstrate the better way to use the optional parameters in function.

Is There a Better Way to Do/Use Optional Function Parameters in JavaScript?

Yes! There are many ways to use the optional parameters in the function. These are as follows:

  • Default parameter value
  • Use the “arguments.length” property

Here, first, we will understand the optional function’s parameters. “Optional parameters” are function parameters that are not needed and may or may not be passed in by the caller.

Example
Define a function named “sum” with two optional parameters “x” and “y” that return the sum of two numbers:

function sum(x, y) {
 return x + y;
}

Call the defined function by passing two numbers “11” and “4” as arguments:

console.log(sum(11, 4));

Pass only one argument “11” as the parameters are optional:

console.log(sum(11));

Here, we will call the function without passing any argument:

console.log(sum());

Now, let’s see the output that gives “NaN” two times instead of throwing an error because there is no other number in the function call to perform the operation:

Now, let’s see the other ways to use the optional parameters.

Method 1: Default Value of the Parameter

Use the “default value” of the parameter to handle optional function parameters. It is the feature of the ES6 module that allows setting a default value for a function parameter in case no value is provided by the caller. It helps to avoid writing additional conditional statements to verify whether a parameter is defined or not.

It is the better way to do optional function parameters as It makes code simple and easy to understand, while also minimizing the possibility of bugs caused by missed or undefined parameters.

Syntax
Follow the given syntax for the default value of the function parameter:

function funcName(a = 5, b = 9) {
 //......
}

Example
Define a function with default value parameters:

function sum(x = 3, y = 5) {
 return x + y;
}

Call the function three times, at the first function call, pass two values “11” and “4” as an argument, in the second call, pass only one argument, and in the last function call, no arguments will be passed:

console.log(sum(11, 4));
console.log(sum(11));
console.log(sum());

The output returns the constant values after performing an operation instead of giving NaN. Because the function call with no arguments and single argument considers default values for performing operation:

Method 2: Use the “arguments.length” Property

Another way to handle optional function parameters, use the “arguments.length” property. Every function in JavaScript has an object called “argument” that holds the parameter values. The “length” of the arguments’ object indicates the total number of parameters provided to the function. In this approach, we will assign a value to unpassed parameters using conditional statements.

Syntax
Following syntax is utilized for the “arguments.length” property to do optional function parameters:

function(a, b, c){
 if(arguments.length == 0){
 // assign the values to all parameters
 } else if(arguments.length == 1){
 // assign the value to the last 2 parameters
 } else if(arguments.length == 2){
 // assign the value to the last parameter
 }
}

Example
Define a function “sum” with two optional variables “x” and “y”. Use the if-else statement to assign values to the parameters based on the argument’s length:

function sum(x, y) {
 if (arguments.length === 0) {
  x = 3;
  y = 5;
 }
 else if (arguments.length === 1) {
  y = 5;
 }
 return x + y;
}

Call the function three times with or without arguments:

console.log(sum(11, 4));
console.log(sum(11));
console.log(sum());

Output

That was all about handling optional function parameters in JavaScript.

Conclusion

The “default value” of the parameter and a “arguments.length” property is used to do/handle optional function parameters. The “default value” of the parameter is a better way to handle optional function parameters as it makes code simple and easy to understand, while also minimizing the possibility of bugs caused by missed or undefined parameters. This blog demonstrated the best way to use the optional parameters in function.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.