Java Script Basics (three) functions
First, the functions in JavaScript
In JavaScript, a function is similar to a method in Java, a block of code that performs a specific function and can be called repeatedly. There are two types of functions in JavaScript, one is a system function and the other is a custom function.
1. System functions
System functions are provided by JavaScript and can be called directly, and commonly used system functions include:
parseint (): Converted to an integer.
Parsefloat (): Converts to floating-point number.
IsNaN (): Determines whether the number is nonzero, returns true for non-digits, and returns false.
Eval (): Evaluates the value of an expression.
2. Custom Functions
To use a custom function, you must first define a custom function.
Grammar:
Function name (parameter 1, parameter 2 ...) {
JavaScript statements
return value; Options available
}
Attention:
1, function is the key word of the functions, must have.
2, the parameters in the function, do not need to use the type, directly use the variable name.
3. The return value in the function is optional, not mandatory, and the return type is determined by the value returned.
Example:
Define a function that calculates the sum of two numbers of functions Add (num1,num2) { return num1 + num2;}
3. Function call
There are two ways to call a function:
1. Event name = function name (parameter value), for example:
onclick= "Add (10,20)";
2, directly using the function name (parameter value), for example:
var result = Add (10,20);
4. Anonymous function
In fact, there is a function called anonymous function, that is, there is no function name.
1, the definition of anonymous functions:
var sum = function (num1,num2) {
return NUM1 + num2;
};
Description
The var sum = function (num1,num2) means that a function implementation is saved in the sum variable.
{} is the implementation of the function, at the end do not forget there is another;
Java Script Basics (three) functions