You can see the example directly, it's too much, it's simple.
(function () {
for (var i=0, len=demoarr.length; i<len; i++) {
if (i = = 2) {
//return; function execution is terminated
//break; The loop is terminated
continue;//loops are skipped
};
Console.log (' demo1arr[' + i + ']: ' + demo1arr[i]);
}
();
For the For loop, there are a few points to note
- I in the For loop still exists after the end of the loop and in the scope, in order to avoid affecting other variables in the scope, use the function itself to isolate () ();
- Avoid using for (Var i=0 i<demo1arr.length; i++) {} so that the length of the array is computed every time, less efficient than the way it was above. You can also perform a variable declaration in front of for to improve reading
- var i = 0, len = demo1arr.length;
- for (; i<len; i++) {};
There are several ways to jump out of the loop
- The return function execution is terminated
- Break loop is terminated
- The continue loop is skipped.
Full example: