JavaScript Default Parameters

The JavaScript default Parameters option was added in the ES6 version of JavaScript. In this tutorial let us learn What is a default Parameter and how to use them in JavaScript

Default Parameters

The Default Parameters allows us to initialize the parameter with default values if no value or undefined is passed as the argument

Need for Default Parameters

JavaScript allows us to pass any number of arguments to a function irrespective of the number of parameters declared by the function

For Example, take a look at the following JavaScript function. We can invoke it without any argument addNum() or with a single argument addNum(1). JavaScript does not prevent us from doing so.

When we do not pass any argument, the JavaScript assigns the value undefined to the parameters. Hence we get NaN as the result.

The Responsibility of checking if the Parameters are provided or not falls on the function itself. There are two ways you can handle it.

One way is to check the value of each parameter and if it is undefined, then raise an error.

Another way is to supply a default value if the parameter is undefined. For our example. we can use 0 as the default value. The code checks each parameter and assigned 0 if undefined.

Instead of checking each parameter, we can make use of the default parameter

Using Default Parameters

The code below assigns 0 to both a & b parameters. The JavaScript checks each argument and if it is undefined then executes the assignment operation.

You can set any value as the Default Value.

Using Expressions as Default Values

You can use also use any expression as a default value. In the example below b takes an expression.

Evaluated at call time

The JavaScript evaluates the default values when we call the function.

In the following example, the a is 1 and b becomes 3 when we call addNum for the first time. We change the value of x & y and when we call the addNum again it will evaluate default values again. Hence a is 5 and b becomes 10.

Using earlier Parameters

You can also make use of earlier parameters in the default value expressions.

In the code below, b uses a in its expression.

But the other way is not possible. The following example results in an error.

This is because JavaScript evaluates the Parameters from left to the right. The expression a=b+1 evaluates first and at that time b is not yet ready. Hence the error.

Passing undefined & null

You can not pass undefined if we use default values. Because, If the value is undefined then JavaScript assigns the default value. No such issue with the null

Hence if you want to pass an undefined value, then do not use default values

Default Parameters can appear anywhere

The default Parameters can appear anywhere in the parameter list.

In the following example, the first two parameters have default values. But the third parameter c does not have one. Here if we want to pass a value to the parameter c, then we need to explicitly pass undefined to a & b.

Function as default Value

You can also use another function as the Default Value.

The showNumber uses another function getNumber to assign a default value to the parameter a

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top