Declare optional function parameters in JavaScript
In this tutorial, we will learn how to declare optional function parameters in JavaScript during defining the function. The optional function parameters can be defined by assigning a default value to the parameter in the function definition. It does not need to be passed when the function is called. The default value will be used if the function is called without assigning a value.
We can do this in different ways to declare optional function parameters in JavaScript with your own custom function.
Using the Assignment Operator (“=”)
We can assign a default value to the parameter by using the Assignment Operator (“=
”) in the function definition.
function myFunc(a, b = 2) { console.log(a, b); } myFunc(4);
In the above program, I have created a function that is myFunc
with two parameters which are a
and b
.
Then assigned a default value 2
to the parameter b
which is an optional parameter.
The console.log()
method is used to print the values of both parameters. After that, I called the function with the argument 4
.
Output:
4 2
As we can see in the above output, the function uses the default value 2
and printed it to the console for the second parameter b
which is an optional parameter.
Now, if we call the function with both parameters like below:
myFunc(4, 7);
Output:
4 7
As we can see in the above output, it printed the value 7
of the parameter b
that is passed when the function is called, instead of using the default value.
Using Logical OR operator (“||”)
We can assign a default value to the parameter by using the Logical OR operator (“||
“) in the function definition.
function myFunc(a, b) { b = b || 2; console.log(a, b); } myFunc(4);
In the above program, the Logical OR operator (“||
“) used to check if the value of b
is truthy or not. If it is truthy, it will assign the value of b
to b
. Otherwise, it will assign the default value 2
to b
.
Output:
4 2
As we can see in the above output, the second parameter b
is not passed when the function is called. The function uses the default value 2 for it. This is because b = b || 2
is checking the truthy value of b
, as b
is not passed when the function is called, its value is undefined, which is falsy and the right side of the operator is executed and assigns the value of the b
into 2.
We can call the function with both parameters like below:
myFunc(4, 7);
Output:
4 7
In the above output, the value 7
of the parameter b
that passed when calling the function has been printed instead of the default value.
Using The Undefined operator
Any parameter that is undefined or has no value passed to a function can be replaced by the default value using a conditional (if
) statement.
function myFunc(a, b) { if (b === undefined) { b = 3; } console.log(a, b); } myFunc(4);
In the above program, the if
statement will check if the value of the parameter b
is undefined
. If the parameter b
is undefined the code inside the if
block will execute which assigns the value of b
into 3.
Output:
4 3
When the function is called with the argument myFunc(4)
, the value of a
is 4 and the value of b
is undefined
so the condition in the if
statement is true, the value of b
is set to 3.
We can also call the function with both parameters like below:
myFunc(4, 7);
Output:
4 7
As we can see in the above output, it printed the value 7
of the parameter b
that is passed when the function is called, instead of using the default value.
Using arguments.length property
An argument object is an array-like built-in object of JavaScript functions that contains all the parameters passed to a function. We will use arguments.length
property to check whether a particular parameter is optional and how many arguments are passed to a function.
function myFunc(a, b) { if (arguments.length < 2) { b = 3; } console.log(a, b); } myFunc(4);
In the above program, I have used the conditional expression if
to check the number of arguments passed to the function. If the number of arguments is less than 2
, it will assign the value of b
into 3
.
Output:
4 3
Like the above we can call the function with both parameters like below:
myFunc(4, 7);
Output:
4 7
In the above output, it printed the value 7
of the parameter b
that is passed when the function is called, instead of using the default value.
Leave a Reply