1. Functions that do not specify a return value return a special undefined value

** return ** Functions that do not specify a return value return a special undefined value.

function add() {
    return;
}
console.log(add()) // undefined
Copy the code

Arguments to the function

The function you define accepts only two arguments, and it is not necessary to pass two arguments when calling the function. One, three, or no arguments can be passed, and the parser never complains. The reason for this is that parameters in ECMAScript are internally represented as an array.

The Arguments object is just like an Array (it is not an instance of Array) in that each of its elements can be queried using square bracket syntax (i.e. the first element is arguments[0], the second is argumetns[1], and so on), The length attribute is used to determine how many parameters are passed in.

Function info (name, age) {console.log(arguments) // arguments (2) ["xl", "19", callee: ƒ, Symbol(symbol.iterator): ƒ] console.log(arguments[0]) // XL console.log(arguments[1]) // 19 console.log(arguments[2]) // undefined console.log(arguments.length) // 2 } info('xl', '19')Copy the code

3. Function parameters are passed by value

Arguments to all functions are passed by value

    1. Example 1:
function setName(obj) { 
 obj.name = "Nicholas"; 
} 
var person = new Object(); 
setName(person); 
alert(person.name); //"Nicholas"
Copy the code
    1. Example 2:
function setName(obj) { 
 obj.name = "Nicholas"; 
 obj = new Object(); 
 obj.name = "Greg"; 
} 
var person = new Object(); 
setName(person); 
alert(person.name); //"Nicholas" 
Copy the code

The only difference between this example and the previous one is that two lines of code have been added to the setName() function: one redefines an object for obj, and the other defines a name attribute with a different value for the object. After passing person to setName(), its name property is set to “Nicholas”. Then, assign a new object to the variable obj with its name attribute set to “Greg”. If person is passed by reference, then person is automatically changed to point to a new object whose name attribute value is “Greg”. However, when you next access person.name, the value is still displayed as” Nicholas”. This means that the original reference remains unchanged even if the parameter values are changed inside the function. In fact, when you override obj inside a function, the variable refers to a local object. The local object is destroyed immediately after the function completes execution