JavaScript Function Parameters
JavaScript functions do not perform any checks on the values of parameters.
Function Explicit Parameters (Parameters) and Implicit Arguments (Arguments)
In previous tutorials, we have learned about the explicit parameters of a function:
functionName
(
parameter1
,
parameter2
,
parameter3
)
{
//
The code to be executed...
}
Explicit parameters of a function are listed when the function is defined.
Implicit parameters of a function 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 parameters.
JavaScript functions do not check the number of implicit parameters.
Default arguments
In ES5, if implicit parameters are not provided when a function is called, the parameters default to: undefined
Sometimes this is acceptable, but it is recommended to set a default value for a parameter:
Example (ES5)
function
myFunction
(
x
,
y
)
{
if
(
y
===
undefined
)
{
y
=
0
;
}
}
Try it »
Or, a simpler way:
Example (ES5)
function
myFunction
(
x
,
y
)
{
y
=
y
||
0
;
}
Try it »
![]() |
If y is already defined, y || 0 returns y because y is true; otherwise it returns 0 because undefined is false. |
|---|
If too many parameters are set when a function is called, the parameters cannot be referenced because the corresponding parameter name cannot be found. They can only be called using the arguments object.
ES6 Functions Can Have Default Parameters
ES6 supports functions with default parameters, which is the operation of judging 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
)
;
//
Outputs 15, the default value of the y parameter
Try it »
The arguments Object
JavaScript functions have a built-in object, the arguments object.
The arguments object contains the array of arguments passed to the function call.
In this way you can easily find the value of the largest argument:
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
;
}
Try it »
Or create a function to sum all the 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
;
}
Try it »
Passing parameters by value
The parameters called in a function are the implicit parameters of the function.
JavaScript implicit parameters are passed by value: the function only obtains the value.
If a function modifies the value of a parameter, it does 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 reference the value of an object.
Therefore, when we modify an object's property inside a function, we modify its initial value.
Modifying an object's property can affect the outside of the function (global variable).
Changes to object properties are visible outside the function.
