Js supplemented with small knowledge points (continue, break, ruturn) and continueruturn
1. continue, break, ruturn
Eg: sum of 1-
$(function (){ $("#hello").click(function () { var iNum = 0; for (var i = 1; i < 101; i++) { iNum += i; } alert(iNum); });});
Result: 5050
Change to break to view the result.
$(function (){ $("#hello").click(function () { var iNum = 0; for (var i = 1; i < 101; i++) { if (i == 5) { break; } iNum += i; } alert(iNum); });});
Result: 10
Conclusion 1: break: jumps out of the entire loop body.
Change to continue and check the result?
1 $(function () 2 { 3 $("#hello").click(function () 4 { 5 var iNum = 0; 6 for (var i = 1; i < 101; i++) 7 { 8 if (i == 5) 9 {10 continue;11 }12 iNum += i;13 }14 alert(iNum);15 });16 });
Result: 5045, (all except 5)
Conclusion 2: continue skips the cycle of the current condition.
Return can be used in either of the following ways:
The first method is to convert it to return to see the result?
The result is: no result. return ends the method body and directly jumps out of the method body, so it cannot be printed.
The second method of return: a method that returns a value
1 $ (function () 2 {3 $ ("# hello "). click (function () 4 {5 var iNum = 0; 6 for (var I = 1; I <101; I ++) 7 {8 if (I = 5) 9 {10 I = A (I); 11} 12 iNum + = I; 13} 14 alert (iNum); 15 }); 16 // The second method of return is to return A value 17 function A (I) 18 {19 I + = 5; 20 return I; 21} 22 });
Result: 5015
Process Analysis:
Conclusion 3: return is used. The first method ends the whole body, and the second method returns a value.