JavaScript function parameters

JavaScript function does not perform any check on the value of the parameter.


Function explicit parameters (Parameters) and implicit parameters (Arguments)

In the previous tutorial, We have learned about the explicit parameters of the function:

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

Explicit parameters of functions are listed when the function is defined.

Implicit function parameters are the real values passed to the function when the function is called.


Parameter rules

When the JavaScript function is defined, the displayed parameter does not specify a data type.

JavaScript functions do not perform type detection on implicit parameters.

The JavaScript function does not detect the number of implicit parameters.


Default parameters

If the function is not called when called Provide implicit parameters, and the parameters will be set by default to: undefined

Sometimes this is acceptable, but it is recommended to set a default value for the parameter:

    php中文网(php.cn) 

设置参数的默认值。

Run the program and try it


Or, a simpler way:

    php中文网(php.cn) 

设置函数参数默认值。

Run the program and try it

Tip: If y has been defined, y || returns y, because y is true, otherwise it returns 0, because undefined is false.


If too many parameters are set when the function is called, the parameters will not be referenced because the corresponding parameter names cannot be found. Can only be called using the arguments object.

Arguments object

JavaScript function has a built-in object arguments object.

The argument object contains the parameter array of the function call.

In this way you can easily find the value of the last parameter:

    php中文网(php.cn) 

查找最大的数。

Run the program to try it

Or create a function to count the sum of all values :

    php中文网(php.cn) 

计算所有参数之和:

Run the program to try it


Pass parameters by value

The parameters called in the function are Implicit parameters of functions.

JavaScript implicit parameters are passed by value: the function just gets the value.

If the function modifies the value of the parameter, the initial value of the explicit parameter (defined outside the function) will not be modified.

Changes to implicit parameters are not visible outside the function.


Passing parameters through objects

In JavaScript, you can reference objects value.

So when we modify the properties of the object inside the function, its initial value will be modified.

Modifying object properties can act outside the function (global variables).

Modifying object properties is visible outside the function.



Continuing Learning
||
php中文网(php.cn)

查找最大的数。

submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!