JavaScript functions
What is a function?
A function is a set of reusable blocks of code. In JavaScript, functions are event-driven or called by other code.
Function is one of the core of JavaScript language. Its basic syntax is as follows:
function functionName(arg0, arg1, ...) {
statements
}
Grammar interpretation
Use the function keyword to define a function
The function keyword is followed by a space followed by the function name
After the function name It is a pair of small brackets. arg0 and arg1 represent the parameters of the function. The parameters are separated by ",". The number of parameters can be 0-25 (0 means no parameters). When there are no parameters, the () brackets cannot be omitted. Parameters exceeding 25 will be ignored by JavaScript
{} is the function body, which contains the functional statements to be implemented by the function
php中文网(php.cn)
JavaScript function naming
Generally, we recommend using camel case naming for function names, and the function name can roughly describe the function or characteristics of the function. The camel case method means that the first character is lowercase and the first letters of other words are capitalized. Here are some examples of function naming:
function changeName(){
...
}
function getAgeByInput(){
...
}
Many people also use underscores to separate words in function names, such as change_name, which is also possible , actual implementation can be based on specific project specifications.
In particular, the _ symbol (such as _getName) is added before the function, which is generally used to represent the private method of the class (object).
Variables within JavaScript functions
#If a variable is declared within a function body, it can only be accessed within that function. This way you can use variables with the same name within different functions.
If you declare a variable outside a function, all functions on the web page can access the variable.
JavaScript function parameters
JavaScript functions allow no parameters (but the parentheses containing the parameters cannot be omitted), and parameters can also be passed to the function for use by the function.
php中文网(php.cn)
Run this example, click the OK button, output:
My name is Xiao Ming, I am 18 years old!
As shown in the above example, when the passed-in value is a string, quotation marks are required; when the passed-in value is a number, quotation marks are not required.
JavaScript function parameter error
JavaScript function parameters do not strictly require which parameters are required parameters and which parameters are optional parameters, so The number of parameters passed in is not allowed to be equal to the number of parameters when defining the function.
If undefined parameters are used in a function, a syntax error (undefined parameters) will be prompted and the JavaScript code will not run normally.
php中文网(php.cn)
Run this example, click the OK button, and output:
My name is Xiao Ming, and I am undefined this year!
JavaScript function return
The return statement of a JavaScript function is used to specify the value returned from the function. When a function is run, to get a running result, you need to use the return statement to return the result.
php中文网(php.cn)
Run this example output:5
Return statement without return value
If the function returns nothing value, you can call the return operator without parameters and exit the function at any time: