JavaScript fundamentals
Theory: Optional function parameters
In programming, many functions and methods have parameters that are rarely changed. In such cases, these parameters are given default values that can be changed as needed. This slightly reduces the amount of identical code.
For example:
The default value looks like a regular assignment in the definition. It only works if the parameter is not passed. This takes some practice to get used to it. The default value can exist even if there is only one parameter:
There can be any number of parameters with default values:
The default values have one limitation. They must go to the very end of the parameter list.
From the syntax point of view, it is impossible to create a function that will have a mandatory parameter next to an optional one:
Recommended programs
Completed
0 / 39

