Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Default Values in functions' arguments

 

One of the features that ES6 provides is the default values in functions' arguments as it shown above. Normally we need to define the arguments for our functions and then replace them with actual values during execution. 

By assigning a default value next to the argument during function declaration, this argument will hold this value as a default, that means if we forget or omit to specify a value for the given argument. Like a fallback value. We can do this for any argument, and the value can be any value we would put while calling the function as before. 

If a value is given to this argument during execution, then the value we have passed will overwrite the default value

Attention: If you have more than arguments with default values, yoou have to be careful as the order matters. For example if you have:

function example(a = 10, b = 5) {

return a + b;

}

example(7) //call the function  like that, overwrites the first argument a to 7 and keep b default. There is no way to change the default of b argument without passing a value to a though!

 

 

Default Values

Kostas Diakogiannis
Module by Kostas Diakogiannis, updated more than 1 year ago
No tags specified