JavaScript Function Parameters
The JavaScript function does not perform any checks on the value of the parameter.
Function explicit Parameters and implicit Parameters(Arguments)
In the previous tutorial, we have learned about the explicit parameters of the function:
functionName(parameter1, parameter2, parameter3) {
// The code to be executed...
}
The explicit parameters of the function are listed when the function is defined.
Function implicit parameters are passed to the real value of the function when the function is called.
Parameter rules
The JavaScript function defines the explicit parameter without specifying the data type.
JavaScript functions do not perform type checking on implicit parameters.
The JavaScript function does not check the number of implicit parameters.
Default parameters
In ES5, if the function does not provide implicit parameters when calling, the parameters will be set to: undefined by default
Sometimes this is acceptable, but it is recommended to set a default value for the parameter:
Example (ES5)
function myFunction(x, y) { if (y === undefined) { y = 0; } }
Or, a simpler way:
Example (ES5)
function myFunction(x, y) { y = y || 0; }
![]() | If y is already defined, y || returns y, because y is true, otherwise it returns 0, because undefined is false. |
---|
If too many parameters are set when the function is called, the parameters will not be quoted because the corresponding parameter name cannot be found. It can only be called using the arguments object.
ES6 functions can take their own parameters
ES6 supports functions with default parameters, and it judges the operation of undefined and ||:
Example (ES6)
function myFunction(x, y = 10) { // y is 10 if not passed or undefined return x + y; } myFunction(0, 2) // output 2 myFunction(5); // output 15, the default value of the y parameter
Arguments object
JavaScript functions have a built-in object arguments object.
The argument object contains an array of parameters for the function call.
In this way, you can easily find the value of the largest parameter:
Example
x = findMax(1, 123, 500, 115, 44, 88); function findMax() { var i, max = arguments[0]; if(arguments.length <2) return max; for (i = 0; i <arguments.length; i++) { if (arguments[i]> max) { max = arguments[i]; } } return max; }
Or create a function to count the sum of all values:
Example
x = sumAll(1, 123, 500, 115, 44, 88); function sumAll() { var i, sum = 0; for (i = 0; i <arguments.length; i++) { sum += arguments[i]; } return sum; }
Pass parameters by value
The parameters called in the function are the implicit parameters of the function.
JavaScript implicit parameters are passed by value: functions just get the value.
If the function modifies the value of the parameter, it will not modify the initial value of the explicit parameter (defined outside the function).
Changes to implicit parameters are not visible outside the function.
Passing parameters through objects
In JavaScript, you can refer to the value of an object.
Therefore, if we modify the properties of the object inside the function, it will modify its initial value.
Modifying object properties can be applied outside the function (global variables).
Modifying object properties is visible outside the function.