How does JavaScript specify default values for parameters
This article introduces the knowledge of "how to assign default values to parameters by JavaScript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Function parameter
Earlier in this tutorial, you learned that functions can have parameters:
FunctionName (parameter1, parameter2, parameter3) {
Code to be executed
}
The function parameter (parameter) refers to the name listed in the function definition.
The function parameter (argument) refers to the real value passed to or received by the function.
Parameter rule
The JavaScript function definition does not specify a data type for parameters (parameter).
The JavaScript function does not perform type checking on the passed parameter (argument).
The JavaScript function does not check the number of parameters received (argument).
Parameter default
If the parameter is omitted when calling the parameter (less than the declared number), the missing value is set to: undefined.
Sometimes this is acceptable, but sometimes it is best to specify a default value for the parameter:
Example
Function myFunction (x, y) {
If (y = undefined) {
Y = 0
}
}
If the function calls too many parameters (more than declared), you can use the arguments object to achieve those parameters.
If a function is called with too many arguments (more than declared), these arguments can be reached using the arguments object.
This is the end of the content of "how to assign default values to parameters by JavaScript". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!