A function is an event-driven or reusable block of code that is executed when he is invoked
Instance
<!DOCTYPE HTML><HTML><Head><Script>functionmyFunction () {alert ("Hello world!");}</Script></Head><Body><Buttononclick= "myFunction ()">Click here</Button></Body></HTML>
Defines a button that executes the MyFunction () function to prompt the user for some information when the button is clicked
JavaScript function syntax
The function is the keyword functions used in front of the block of code wrapped in curly braces:
For example:
function MyFunction () { Here is the code block to execute }
Code blocks inside a function are executed when the function is called
You can call a function directly when something happens (such as when a user taps a button) and can be called by JavaScript at any location
JavaScript is case sensitive. The keyword function must be lowercase and must be called with the same casing as the function name.
Calling a function with parameters
When you call a function, you can pass a value to it, which is called a parameter.
These parameters can be used in functions.
You can send any number of arguments separated by commas (,):
MyFunction (Argument1,argument2)
When you declare a function, change the argument to a variable to declare it.
function MyFunction (VAR1,VAR2) {Here is the code to execute}
Variables and parameters must appear in a consistent order the first variable is the given value of the first passed argument and so on
Instance:
<Body> <Buttononclick= "myFunction (' Tian ', ' CEO ')">Dot me.</Button> <Script> functionmyFunction (name,job) {alert ("Welcome" +name+", the" +job); } </Script> </Body>
function with return value
Sometimes, we want the function to return the value to the place where it was called.
This can be achieved by using the return statement.
When you use the return statement, the function stops execution and returns the specified value.
function myFunction () {var x=5;return x;}
The above function returns the value 5.
Note: The entire JavaScript does not stop executing, just the function. JavaScript will continue to execute code from where the function is called.
The function call will be replaced by the return value:
var myvar=myfunction ();
The value of the MyVar variable is 5, which is the value returned by the function "MyFunction ()".
You can use the return value even if you do not save it as a variable:
document.getElementById ("Demo"). Innerhtml=myfunction ();
The InnerHTML of the "demo" element will be 5, which is the value returned by the function "MyFunction ()".
You can make the return value based on the arguments passed to the function:
Instance
Calculates the product of two numbers and returns the result:
function MyFunction (A, b) {return a*b;} document.getElementById ("Demo"). Innerhtml=myfunction (4,3);
The InnerHTML of the "demo" element will be: 12
You can also use the return statement when you just want to exit the function. The return value is optional:
function MyFunction (A, B) {if (a>b) { return; } X=A+B}
If a is greater than B, the above code exits the function and does not calculate the sum of a and B.
Local JavaScript variables
A variable declared inside a JavaScript function (using VAR) is a local variable, so it can only be accessed inside the function. (The scope of the variable is local).
You can use local variables with the same name in different functions, because only the function that declares the variable can recognize the variable.
As soon as the function is complete, the local variable is deleted.
Global JavaScript variables
Variables declared outside of a function are global variables, and all scripts and functions on a Web page can access it.
The lifetime of a JavaScript variable
The lifetime of the JavaScript variables starts at the time they are declared
Local variables are deleted after the function is run
Global variables are deleted after the page is closed.
Assigns a value to an undeclared JavaScript variable.
If you assign a value to a variable that has not been declared, the variable is automatically declared as a global variable
Carname= "Volvo";
A global variable will be declared Carname even if he executes inside the function.
HTML Learning Note JavaScript (function)