GoConqr has a new look! Let us know what you think.

Module 3 of 10 · Media

Default Values

Kostas Diakogiannis

Default Values

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!

 

 

Instructions

You must submit an attempt for this quiz to progress. You can submit it after the last question.

Go to the chosen module

Feedback

What is it about?

What do you think of the new look?

How do you like it?