JavaScript function
Function is event-driven or reusable blocks of code executed when it is called.
Examples
<html>
<head>
<script>
function myFunction()
{
alert("Hello World!");
}
</script>
</head>
<body>
<button onclick=" myFunction() ">Try it</button>
</body>
</html>
JavaScript function syntax
Function block is wrapped in braces, and previously used the keyword function:
{
执行代码
}
When the function is called, it will execute the code within the function.
It can directly call the function when an event occurs (such as when the user clicks the button), and JavaScript can be called at any position.
JavaScript is case-sensitive. Keywords function must be lowercase, and must function with the same name as the case to call the function. |
Function calls with arguments
When calling the function, you can pass it a value, these values are called arguments.
These parameters can be used in the function.
You can send any number of parameters, separated by a comma (,):
When you declare the function, the parameters as variables declaration:
{
代码
}
Variables and parameters must appear in the same order. The first variable is the first passed parameter given value, and so on.
Examples
<script>
function myFunction( name , job )
{
alert("Welcome " + name + ", the " + job );
}
</script>
try it"
The above function button is clicked prompts "Welcome Harry Potter, the Wizard".
Function is very flexible, you can use different parameters to call this function, this will give a different message:
Examples
<button onclick="myFunction( 'Bob' , 'Builder' )">Try it</button>
try it"
According to the different buttons you click, the above example will prompt "Welcome Harry Potter, the Wizard" or "Welcome Bob, the Builder".
Function returns a value with
Sometimes we want the function to return a value to the calling function.
By using the return statement can be achieved.
When using the return statement, the function stops execution and returns the specified value.
grammar
{
var x=5;
return x;
}
The above function returns a value of 5.
Note: The entire JavaScript does not stop execution, just function. JavaScript code execution will continue from where the function is called.
Function calls will be returned value substituted:
The value of myVar variable is 5, that is, "myFunction) (" the value returned by the function.
If not save it as a variable, you can use the return value:
innerHTML "demo" element will be 5, that is, "() myFunction" value returned by the function.
You can make the return value passed to a function based on the parameters:
Examples
The product of two numbers and returns the result:
{
return a*b;
}
document.getElementById("demo").innerHTML=myFunction(4,3);
innerHTML "demo" element will be:
try it"
When you just want to exit the function, the return statement may also be used. The return value is optional:
function myFunction(a,b) { if (a>b) { return; } x=a+b }
If a is greater than b, then the above code will exit the function and does not calculate the sum of a and b.
Local JavaScript Variables
JavaScript variable declared inside a function (using var) is a local variable, so it can only be accessed within the function. (Scope of the variable is local).
You can use the same local variable name in different functions, because only declared the function of the variable to identify the variable.
As long as the function is completed, the local variable will be deleted.
Global JavaScript variable
Variables declared outside a function is a global variable that all scripts and functions on the page can access it.
Survival of JavaScript variables
Life of JavaScript variables from the time they are declared to start.
Local variables will be deleted after the function is run.
Global variables are deleted after the close of the page.
Assign values to JavaScript variables undeclared
If you assign a value to a variable has not been declared, the variables will be automatically declared as global variables.
This statement:
We will declare a global variable carname, even if it is performed within the function.