A function is a set of statements that complete a particular function. Without a function, it may take five lines, 10 rows, or even more code to complete the task. In this case, we can put the code block that completes the specific function into a function, call this function directly, save the trouble of repeatedly inputting a lot of code.
How do you define a function? The basic syntax is as follows:
function name () {function code;}
Description
1. function defines the keyword for the functions.
2. "Function name" you take the name of the function.
3. "Function code" is replaced with code that completes a specific function.
Let's write a simple function that implements the addition of two numbers and give the function a meaningful name: "ADD2", the code is as follows:
function Add2 () { var sum = 3 + 2; alert (sum);}
Function call:
Once the function is well defined, it cannot be executed automatically, so it needs to be called, just write the function directly where it is needed, and the code is as follows:
javascript--what is a function