A function is a block of code enclosed in curly braces. The keyword function is used before:
Function functionname () {the code to be executed here}
When this function is called, the code in the function is executed.
A function can be called directly when an event occurs (for example, when a user clicks a button), and JavaScript can call the function anywhere.
Tip: JavaScript is case sensitive. The keyword function must be in lower case and must be in the same case as the function name.
Call a function with Parameters
When calling a function, you can pass values to it. These values are called parameters.
These parameters can be used in functions.
You can send any number of parameters separated by commas:
myFunction(argument1,argument2)
When you declare a function, declare the parameter as a variable:
Function myFunction (var1
,var2
) {Here is the code to be executed}
The variables and parameters must appear in the same order. The first variable is the given value of the first passed parameter, and so on.
Instance
<Button onclick = "myFunction ('bill Gates', 'ceos ')"> click here </button> <script> function myFunction (name, job) {alert ("Welcome" +name
+ ", The" +job
) ;}</Script>
The above function prompts "Welcome Bill Gates, the CEO" when the button is clicked ".
The function is flexible. You can use different parameters to call the function, so that different messages are displayed:
Functions with return values
Sometimes, we want the function to return the value to the place where it is called.
You can use the return statement.
When the return statement is used, the function stops execution and returns the specified value.
Syntax
function myFunction(){var x=5;return x;
}
The above function returns 5.
Note: The entire JavaScript operation will not stop. It is just a function. JavaScript will continue to execute the code from where the function is called.
Function calls will be replaced by return values:
var myVar=myFunction();
The value of myVar is 5, that 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, that is, the value returned by the function "myFunction.
You can make the return value based on the parameters passed to the function:
Instance
Calculate the product of two numbers and return 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 only 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 from the function and does not calculate the sum of a and B.
Local JavaScript Variables
The variable declared inside the JavaScript function (using var) isLocalVariable, so you can only access it within the function. (The scope of this variable is local ).
You can use local variables with the same name in different functions, because only functions that have declared the variable can recognize the variable.
After the function is run, the local variable is deleted.
Global JavaScript Variables
The variable declared outside the function isGlobalVariable. All scripts and functions on the web page can access it.
Survival of JavaScript Variables
The lifecycle of JavaScript variables starts from the time they are declared.
Local variables will be deleted after the function is run.
Global variables will be deleted after the page is closed.
Assign values to undeclared JavaScript Variables
If you assign a value to a variable that has not been declared, the variable is automatically declared as a global variable.
This statement:
carname="Volvo";
DeclareGlobalVariable carname, even if it is executed in the function.