I. Overview of functions:
1, the function is defined once but can call or execute any number of times the JS code.
2, the function sometimes has parameters, that is, when the function is called to specify the value of the local variable.
3. Functions often use these parameters to calculate a return value, which is also the value of a function call expression. ( simply a block of code that accomplishes a particular function ).
4 . In JavaScript, the function type is actually an object. Each of these functions is an instance of a function type and has properties and methods as well as other reference types.
5, because the function is an object, the function name is actually a pointer to the function object.
6, function for any language is a core concept. Functions can encapsulate any number of statements, and can be invoked anywhere, at any time, to execute.
7. Functions in JavaScript are declared using the function keyword followed by a set of parameters and the body of the function.
Second, the function of the declaration:
1. functions with no parameters
function Box1 () { alert (' only function is called, I am executed '// directly called function, the function itself does not execute itself and needs to be called to execute
2. Functions with Parameters
function box (name, age) { alert (' Your name: ' +name+ ', Ages: ' + ');} Box ("Huang Jianfeng", 22); // call the function, and pass the parameter, if does not pass the argument, also does not have the error, but the two parameter's value is undefined
return value
Functions with and without parameters above do not have a return value defined, but are executed directly after the call. In fact, any function can implement the return value with the return statement followed by the value to be returned.
function box () { return// returns the function's final value by return/ / call function to get the return value, Then the alert output
function box (name, age) { return ' Your name: ' +name+ ', Ages: ' +age; Returns the final value of the function by return }alert (the box (/// call function Gets the return value, then the outer output
You can also assign the return value of a function to a variable and then manipulate it through a variable.
function box (NUM1, num2) { return NUM1 * num2;} var // The return value given by the function is assigned to the variable alert (num);
The return statement also has a function of exiting the current function, noting the difference between break and break: The break is used in the Loop and switch branch statements
function box (num) { if (num < 5) return// satisfies the condition, returns NUM, returns, Do not execute the following statement, return ; Alert (Box (2));
JavaScript functions overview, declarations, return values