JavaScript

Set a Default Parameter Value for a JavaScript Function

In JavaScript functions, the parameters are variables declared in the function’s definition that receive input from the caller. Similarly, arguments are the variables passed to the function when it is invoked/called. The term “default parameter” is a feature that allows to specify the default values for a function’s parameters.

When a function is invoked without passing an argument, the default value is utilized. Moreover, setting the default parameter values can make code more robust, simpler, and easier to read and understand, especially when dealing with optional parameters.

This blog will describe the procedure for setting the parameter’s default value for a function in JavaScript.

How to Set a Default Parameter Value for a JavaScript Function?

To set the default parameter value for a function in JavaScript, use the “=” operator in the function definition.

Syntax

For setting the default parameter value to a function, use the following syntax:

function functionName(param = defaultValue){

//....

}

Example

Define a function “sum” with three parameters “a”, ”b” and “c” and setting the default parameter values to “7”, “2”, ”4”, respectively:

function sum(a = 7, b = 2, c = 4) {

return a + b + c;

}

Call the defined function “sum()” without passing any arguments. It will give the sum of the default values of parameters:

console.log(sum());

Now, we will overwrite the default values by passing “1”, “4”, and “6” as arguments. It will return the sum of these provided arguments:

console.log(sum(1, 4, 6));

Here, pass the arguments “4” and “3”, which act as a value of the variables “a” and “b”. For “c”, the default value will be used:

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

Output

You can also set the default value of the parameter inside the function body by identifying whether the type of variable is undefined. If yes, then set the default value of that variable:

function sum(a, b = 2, c = 4) {

if (typeof a === 'undefined') a = 9;

return a + b + c;

}

Call the function that will give the sum of all the default values of “a”, “b”, and “c” which are “9”, “2” and “4” respectively:

console.log(sum());

Overwrite the values of the parameters and find the sum of all these arguments:

console.log(sum(1, 4, 6));

Output

That’s all about setting the default parameter value for JavaScript function.

Conclusion

For setting the default parameter value for a function in JavaScript, use the “=” operator in the function definition. These default values are used at the time of calling the function where the arguments are not passed. You can also set the default values inside the function body. This blog described the procedure for setting the default parameter value for a function in JavaScript.

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.