Functions jointly define the same processing and make it available for multiple uses. The following article will introduce you to the usage of functions in How to use How to use JavaScript functions functions.
If there is a lot of code, sometimes you may need to use a lot of the same processing. Although you can save time by copying and pasting, the code will become very long. , it is inconvenient for some subsequent operations. At this time, we can define the same processing as a function, and then call this function, so that the code will look very concise.
In addition, the function also has an advantage. When part of the code that needs to be processed needs to be changed together, only one place needs to be changed.
Let’s look at the definition of function in How to use How to use JavaScript functions functions
Basic form
The first thing you need when defining a function is function . Then write the name of the function and write the required processing in {}. The simplest way to write it is as follows<script> function 函数名( ) { 处理的代码 } </script>
Parameters and return values
<script> function 函数名(参数1,参数2,...){ 处理的代码 return 返回值; } </ script>
specific examples of using functional programming
We first use variables to write, define the variable as money, according to the value of the input money, use document.write outputs three types of strings: "rich people", "ordinary people", and "poor". The code is as follows<!DOCTYPE html> <html> <head> <meta charset = "utf-8"> <title>How to use How to use JavaScript functions functions</title> </head> <body> <script> var money; if (money > 5000) { document.write("有钱人"); } else if (money > 3000){ document.write("普通人"); } else { document.write("贫穷"); } </script> </body> </html>
<script> var money=10000; if (money > 5000) { document.write("有钱人"); } else if (money > 3000){ document.write("普通人"); } else { document.write("贫穷"); } </script>
specific examples of using functions
The function name here is judge. Because I want to judge "rich people", "ordinary people" and "poor people" based on the value of the input variable money. Actually, you can't tell what information is passed to the function, so the variable money is used to define the parameters. This function will change the output value and string based on the value entered into the variable money.function judge(money) { if (money > 5000) { document.write("有钱人"); } else if (money > 3000){ document.write("普通人"); } else { document.write("贫穷"); } }
<script> function judge(money) { if (money > 5000) { document.write("有钱人"); } else if (money > 3000){ document.write("普通人"); } else { document.write("贫穷"); } } var money = 10000; judge(money); var money = 4000; judge(money); var money = 2000; judge(money); </script>
Use the return value to return
You can also return the string of the judgment result without using document.write. . In this case, use the return value return. After substituting the judgment result strings "rich", "ordinary" and "poor", finally use return to return the value of the result variable. The code is as follows<script> function judge(money) { var result; if (money > 5000) { result = "有钱人"; } else if (money > 3000){ result = "普通人"; } else { result = "贫穷"; } return result; } var money = 10000; var result = judge(money); document.write(result); var money = 4000; var result = judge(money); document.write(result); var money = 2000; var result = judge(money); document.write(result); </script>
##Finally, let’s take a brief look
What is local Variables
Local variables refer to variables defined in a function and only have a role in that function.
Using "var variable name" declaration is no different from ordinary variables, but in the case of local variables, it is declared in the function.
The above is the detailed content of How to use JavaScript functions. For more information, please follow other related articles on the PHP Chinese website!