Default function parameters in vanilla JavaScript

Today, we’re going to look at how you can set default values for function parameters when none are provided.
Let’s dig in!
The danger of missing parameters Let’s imagine you have an add() function that adds two numbers together.
/** * Add two numbers together * @param {Number} num1 The first number * @param {Number} num2 The second number * @return {Number} The sum of both numbers */ function add (num1, num2) { return num1 + num2; } If you don’t pass in a value for a function parameter, the parameter has a value of undefined.


This content originally appeared on Go Make Things and was authored by Go Make Things

Today, we’re going to look at how you can set default values for function parameters when none are provided.

Let’s dig in!

The danger of missing parameters

Let’s imagine you have an add() function that adds two numbers together.

/**
 * Add two numbers together
 * @param  {Number} num1 The first number
 * @param  {Number} num2 The second number
 * @return {Number}      The sum of both numbers
 */
function add (num1, num2) {
	return num1 + num2;
}

If you don’t pass in a value for a function parameter, the parameter has a value of undefined. This can result in unexpected behavior.

For example, passing just a single number into our add() functions results in a returned value of NaN, which stands for Not a Number.

// returns "NaN"
add(3);

Default function parameters provide an easy way to handle this.

Default function parameters

You can set a default value for each parameter by adding = default value when defining it.

In this example, both num1 and num2 will get a value of 0 is no argument is supplied.

/**
 * Add two numbers together
 * @param  {Number} num1 The first number
 * @param  {Number} num2 The second number
 * @return {Number}      The sum of both numbers
 */
function add (num1 = 0, num2 = 0) {

	// Add the numbers
	// If num1 or num2 is not provided, 0 will be used
	return num1 + num2;

}

// returns 4
add(4);

Default function parameters and assignment order

One best practice with default function parameters is that any parameter that has one should appear after regularly assigned parameters (ones without a default value).

Don’t do this, for example.

/**
 * Add two numbers together
 * @param  {Number} num1 The first number
 * @param  {Number} num2 The second number
 * @return {Number}      The sum of both numbers
 */
function add (num1 = 0, num2) {
	return num1 + num2;
}

Browsers will still run the code just fine, but many JavaScript linters will throw a warning or error.

The logic behind it is that parameters with a default values are likely optional, and should appear after parameters that are not.

⏰🦉 Early Bird Sale! Today through Monday, get 40% off registration in the next session of the Vanilla JS Academy.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2023-03-14T14:30:00+00:00) Default function parameters in vanilla JavaScript. Retrieved from https://www.scien.cx/2023/03/14/default-function-parameters-in-vanilla-javascript/

MLA
" » Default function parameters in vanilla JavaScript." Go Make Things | Sciencx - Tuesday March 14, 2023, https://www.scien.cx/2023/03/14/default-function-parameters-in-vanilla-javascript/
HARVARD
Go Make Things | Sciencx Tuesday March 14, 2023 » Default function parameters in vanilla JavaScript., viewed ,<https://www.scien.cx/2023/03/14/default-function-parameters-in-vanilla-javascript/>
VANCOUVER
Go Make Things | Sciencx - » Default function parameters in vanilla JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/14/default-function-parameters-in-vanilla-javascript/
CHICAGO
" » Default function parameters in vanilla JavaScript." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2023/03/14/default-function-parameters-in-vanilla-javascript/
IEEE
" » Default function parameters in vanilla JavaScript." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2023/03/14/default-function-parameters-in-vanilla-javascript/. [Accessed: ]
rf:citation
» Default function parameters in vanilla JavaScript | Go Make Things | Sciencx | https://www.scien.cx/2023/03/14/default-function-parameters-in-vanilla-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.