JavaScript Function Parameters
JavaScript functions do not perform any checks on the value of the parameters.
Explicit Parameters and Implicit Arguments
In previous tutorials, we have learned about explicit parameters of a function:
functionName(parameter1, parameter2, parameter3) {
// Code to be executed...
}
Explicit parameters are listed in the function definition.
Implicit arguments are the actual values passed to the function when it is invoked.
Parameter Rules
JavaScript function definitions do not specify data types for 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 a function is called without implicit arguments, the parameters are set to: undefined
Sometimes this is acceptable, but it is recommended to set a default value for the parameters:
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 defined, y || 0 returns y, because y is true, otherwise it returns 0, because undefined is false. | | --- | --- |
If a function is called with too many arguments, the extra arguments cannot be referenced because they do not have corresponding parameter names. They can only be accessed using the arguments object.
ES6 Function with Default Parameters
ES6 supports functions with default parameters, eliminating the need for checking undefined and the || operation:
Example (ES6)
function myFunction(x, y = 10) {
// y is 10 if not passed or undefined
return x + y;
}
myFunction(0, 2) // Outputs 2
myFunction(5); // Outputs 15, default value of y
The arguments Object
JavaScript functions have a built-in object called the arguments object.
The arguments object contains an array of the arguments used when the function was called.
This way, you can easily find the largest value of the arguments:
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 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;
}
Passing Parameters by Value
The parameters called within a function are the implicit arguments of the function.
JavaScript implicit arguments are passed by value: the function only gets the value.
If the function changes the value of a parameter, this change does not affect the initial value (defined outside the function).
Changes to implicit arguments are not visible outside the function.
Passing Parameters by Object
In JavaScript, values of objects can be referenced.
Therefore, modifying properties of an object within a function modifies its initial values.
Changes to object properties are visible outside the function (global scope).
Changes to object properties are visible outside the function.