English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
JavaScript global properties
A function can have one or more parameters, which will be provided by the calling code and can be used within the function.
FunctionFunction parameters and actual argumentsParametersname is the name listed in the function definition..
FunctionActual argumentsare the actual arguments passed to the function (and received by the function).Value.
When defining a function to accept input values at runtime, you can specify parameters.
Function parameters are listed within the parentheses () in the function definition.
// However, for each parameter you specify, you must pass the corresponding parameter to the function when calling it; otherwise, its value will become uncertain. function greet(name) { document.write("Hello, ") + document.write("Hello, ") } // name); Call the greet function with "Seagull" as a parameterTest and see‹/›
greet("Seagull");
// However, for each parameter you specify, you must pass the corresponding parameter to the function when calling it; otherwise, its value will become uncertain. You can define any number of parameters as needed.1function add(num2function add(num3) { , num1 + var total = num2 + var total = num3num ; } // Call a function 0,5, 2document.write(total); 10); // add( 35 0,-5, 8, 7); // add( 10Test and see‹/›
Output:
//However, for each parameter you specify, you must pass the corresponding parameter to the function when calling it; otherwise, its value will become uncertain. Define a function function showFullname(fName, lName) { + " " + lName); } // Call a function showFullname("Kavy", "Mark"); // Output: Kavy Mark showFullname("John"); // Output: John undefinedTest and see‹/›
ifundefinedpassedValueorundefinedthenDefault function parametersNamed parameters can be initialized with default values.
This means that if no parameters are provided when calling the function to use these functions, then these default parameter values will be used.
function myFunc(a, b = 20) { // If parameter b is not passed or undefined, then b is20 return a + b; } myFunc(10, 5); // Return 15 myFunc(10);// Return 30Test and see‹/›
All functions in JavaScript can use the arguments object by default. The arguments object includes the values of each parameter.
The arguments object is an array-like structure. You can access its values using array-like indices.
The following example returns the maximum number from the passed arguments:
getMax(9, 15, 61, 3); function getMax() { let max = 0; for (let i = 0; i < arguments.length; i++) { if (arguments[i] > max) { max = arguments[i]; } } return max; }Test and see‹/›
The following example returns the sum of all passed arguments:
getSum(9, 15, 61, 3); function getSum() { let sum = 0; for (let i in arguments) { sum += arguments[i]; } return sum; }Test and see‹/›
Even if the function does not contain any parameters, the arguments object is still valid.
The arguments object is similar to an array of objects, but it does not support array methods.
Reset parameterThe syntax allows our parameters to be passed to a function as an array of indefinite number.
This is particularly useful when you want to pass parameters to a function but do not know how many parameters are needed.
Specify rest parameters by adding the rest operator (...), which is three dots, in front of named parameters.
function sum(...arr) { return arr.reduce((previous, current) => { return previous + current; }); } sum(1, 2); // Return 3 sum(1, 2, 3, 4);// Return 10 sum(1, 2, 3, 4, 5, 6); // Return 21Test and see‹/›
In the function call, parameters are the function's parameters.
JavaScript parameters are passed byValuePassing: the function only knows the value, not the position of the parameter.
If the function changes the value of the parameter, it will not change the original value of the parameter.
Changes to parameters are not visible outside the function (reflected).
In JavaScript, object references are values.
Therefore, the object behaves as if it is passed throughReferencePassing the same:
If the function changes the object property, it will change the original value.
Changes to object properties are visible outside the function (reflected).