Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JavaScript function parameters


May 06, 2021 JavaScript


Table of contents


JavaScript function parameters


The JavaScript function does not check the value of the arguments.

The difference between JavaScript function parameters and function parameters in most other languages is that it does not care how many parameters are passed, and does not care about the data type of the parameters passed.


Function Explicit Arguments and Hidden Arguments

In previous tutorials, we've looked at explicit parameters for functions:

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

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

The function hides arguments that are passed to the true value of the function when it is called.


Argument rules

The JavaScript function is defined with parameters that do not specify a data type.

The JavaScript function does not detect hidden arguments.

The JavaScript function does not detect the number of hidden arguments.


The default parameters

If the function is missing arguments at the time of call, the argument is set to: undefined by default

Sometimes this is acceptable, but it is recommended that you set a default value for the parameters:

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

Try it out . . .

Or, in a simpler way:

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

Try it out . . .
JavaScript function parameters If y is already defined, y || 0 returns y, because y is true, otherwise 0 is returned, because undefined is false.

If too many arguments are set when the function is called, the argument cannot be referenced because the corresponding argument name cannot be found. Only arguments objects can be called.


Arguments object

The JavaScript function has a built-in object arguments object.

The argument object contains an array of arguments called by the function.

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

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

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

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


Pass parameters by value

The argument called in the function is the argument to the function.

If the function modifies the value of the argument, the initial value of the argument (defined outside the function) is not modified.

Summary: The JavaScript function simply passes the value of the argument into the function, and the function configures the memory to hold the parameter value separately, so it does not change the value of the original argument.

Instance

var x = 1;
// 通过值传递参数
function myFunction(x) {
    x++; //修改参数x的值,将不会修改在函数外定义的变量 x
    console.log(x);
}
myFunction(x); // 2
console.log(x); // 1

Pass parameters through objects

In JavaScript, you can reference the value of an object.

So we modify the properties of an object inside the function to modify its initial value.

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

Instance

var obj = {x:1};
// 通过对象传递参数
function myFunction(obj) {
    obj.x++; //修改参数对象obj.x的值,函数外定义的obj也将会被修改
    console.log(obj.x);
}
myFunction(obj); // 2
console.log(obj.x); // 2

Tip: You can practice using JavaScript function parameters in this site's JavaScript programming practice!