The best JavaScript function parameters Tutorial In 2024, In this tutorial you can learn Explicit function parameters (Parameters) and implicit parameters (Arguments),Parameter rule,The default parameters,Examples,Examples,Arguments Object,Examples,Examples,Passing parameters by value,Pass parameters through the object,

JavaScript function parameters

JavaScript function parameter value without any examination.


Explicit function parameters (Parameters) and implicit parameters (Arguments)

In the previous tutorial, we have learned an explicit function parameters:

functionName (parameter1, parameter2, parameter3) {
// Code to be executed /en/en/en
}

Function parameters explicitly listed in the function definition.

Implicit function arguments passed to a function when the real value of the function call.


Parameter rule

JavaScript function definition display parameter is not specified data type.

JavaScript function implicit type parameter is not detected.

JavaScript function of the number of implicit parameter is not detected.


The default parameters

If the function is not available when you call an implicit parameter, the parameter is set to default: undefined

Sometimes this is acceptable, but it would be best to set a default value for a parameter:

Examples

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

Or, easier way:

Examples

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

Note If y has been defined, y || return y because y is true, otherwise it returns 0, because the undefined is false.

Too many parameters If the function is set, the parameters will not be quoted, because they can not find the corresponding parameter name. Use only the arguments object to call.


Arguments Object

JavaScript function has a built-in objects arguments object.

argument object contains a parameter array function call.

You can easily find the value of the last parameter in this way:

Examples

x = findMax (1, 123, 500, 115, 44, 88); function findMax () { var i, max = 0; for (I = 0;. I < arguments length; i ++) { if (Arguments [i]> max) { max = arguments [i];} } return max;}

Or create a function to count all values ​​and:

Examples

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

Arguments in the function call is an implicit function of the parameters.

JavaScript implicit parameter passed by value: function just to get the value.

If the function to modify the value of a parameter, it does not modify the initial value of the explicit parameter (defined outside a function).

Change implicit argument outside the function is not visible.


Pass parameters through the object

In JavaScript, the value can be referenced object.

Therefore, we modify an object's properties within the function will modify its initial value.

Modify object properties outside the function can be applied to (global variables).

Modify object properties outside the function is visible.

JavaScript function parameters
10/30