Sort out the Javascript function learning notes. A series of previous articles are about the study of Javascript. This article is about the further study of Javascript Functions. I hope you will continue to pay attention to it.
1. What is a function?
If you need to use the same piece of code multiple times, you can encapsulate them into a function. A function is a set of statements that can be called in your code at any time. Each function is actually a short script.
For example, you need to complete the functions of multiple groups.
Var sum; sum = 3 + 2; alear (sum); sum = 7 + 8; alear (sum );
If you want to implement the sum of eight groups, you need 16 lines of code. The more you implement, the more lines of code. Therefore, we can put the code block that completes a specific function into a function and directly call this function to save the trouble of repeatedly inputting a large amount of code. Function completion:
Function add (a, B) {sum = a + B; // you only need to write it once.}; add2 (); add2 );...... // you only need to call the function.
2. define functions
Define function syntax
Function Name (parameter argument) {function body statements;} // function-Defined function keywords. You can use the function name as the function name, replace the "function body" with the code to complete a specific function.
Function shout () {var beatles = Array ("John", "Paul", "George", "Ringo"); for (var count = 0; count <beatles. length; count ++) {alert (beatles [count]) ;}// the loop statements in this function will pop up in sequence to display the content in beatles. Shout (); call the function to execute the action in the script
To sum two numbers and display the results: