JavaScript Function Parameters
JavaScript functions do not perform any checking on the values of parameters.
Function Explicit Parameters and Implicit Arguments
In earlier tutorials, we have already learned about explicit parameters of functions:
Function explicit parameters are listed when the function is defined.
Function implicit arguments are the actual values passed to the function when it is called.
Parameter Rules
JavaScript functions do not specify data types when defining explicit parameters.
JavaScript functions do not perform type checking on implicit arguments.
JavaScript functions do not check the number of implicit arguments.
Default Parameters
In ES5, if the function is called without providing implicit arguments, the parameter will default to:undefined
Sometimes this is acceptable, but it is recommended to set a default value for the parameter:
Or, an even simpler way:
![]() |
If y is already defined,y || 0return y, because y is true; otherwise return 0, because undefined is false. |
|---|
If too many arguments are provided when calling a function, the extra arguments cannot be referenced because no corresponding parameter name can be found. They can only be accessed using the arguments object.
ES6 Functions Can Have Default Parameters
ES6 supports functions with default parameters, which is the operation of checking undefined and ||:
Example (ES6)
Try it Yourself »
The arguments Object
JavaScript functions have a built-in object, the arguments object.
The arguments object contains the array of arguments for a function call.
In this way, you can easily find the value of the largest argument:
Example
Try it Yourself »
Or create a function to sum all the values:
Example
Try it Yourself »
Passing Parameters by Value
The arguments passed when calling a function are the function's implicit arguments.
JavaScript implicit arguments are passed by value: the function only gets the value.
If the function modifies the value of a parameter, it will not modify the initial value of the explicit parameter (defined outside the function).
Changes to implicit arguments are not visible outside the function.
Passing Parameters by Object
In JavaScript, you can reference the value of an object.
Therefore, if we modify an object's property inside a function, it modifies its original value.
Modifying object properties can affect the outside of the function (global variables).
Modifications to object properties are visible outside the function.
Other Extensions