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:

functionName(parameter1, parameter2, parameter3) { //The code to be executed... }

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:

Example (ES5)

function myFunction(x, y) { if (y === undefined) { y = 0; } }

Try it Yourself »

Or, an even simpler way:

Example (ES5)

function myFunction(x, y) { y = y || 0; }

Try it Yourself »

Note 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)

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 parameter y

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

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 Yourself »

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 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