FunctionIs a javascript code that is defined once but can be called or executed multiple times. A function can have parameters. A function often uses these parameters to calculate a return value. This value is also called the value of a function call expression. When a function is called on an object, it is called a method. Note: when defining a function, you can use a variable number of parameters, and the function can either have a return statement or no return statement. The return statement can stop the function and return the expression value to the function caller. If the return statement does not have a related expression, it returns undefined. If a function does not contain a return statement, it only executes each statement in the function body and returns it to the caller undefined.
Because Javascript is a loose language, you cannot specify a data type for function parameters, and JavaScript does not check whether the passed data is the type required by that function. If the data type of a parameter is very important, you can use the typeof operator to detect it. JavaScript does not check whether the number of parameters passed to it is correct. If more parameters are passed than the number required by the function, the excess values are ignored. If the number of parameters passed is less than the number required by the function, the ignored parameters will be assigned an undefined value.
Function quantity:
Javascript allows you to define a function using the function quantity directly. A function is an expression that defines an anonymous function. The following two lines of code use the function statement and function to define two functions that are basically the same.
function f(x){return x*x} //function statementvar f=function(x){return x*x} //function literal
Although a function is directly created with an unnamed function, its syntax also stipulates that it can specify the function name, which is very useful in writing and calling its own recursive function. For example:
var f=function fact(x){if(x<=1) return 1;else return x*fact(x-1)}
The code above defines an unnamed function and stores its reference in variable F. It does not actually store reference to a function in a variable named fact, but only allows the function body to reference itself with this name.
Because functions are directly created by JavaScript expressions, rather than statements, they are more flexible to use, especially for those functions that only use once and do not need to be named. For example, a function specified using a function's direct amount expression can be stored in a variable, passed to other functions, or even called directly:
f[0]=function(x){return x*x;}; //define a function and store ita.sort(function(a,b){return a-b;}); //define a function;pass it to anothervar tensquared=(function(x){return x*x;}) (10); //define and invoke
Function Name:
Any valid JavaScript identifier can be used as a function name. Use descriptive and refined function names as much as possible. The function name is typically prefixed with lowercase letters. When a name contains multiple words, it can be named by underscore or hump. For exampleLike_this ()
AndLikethis ().