The break
and continue
can be used in JavaScript with loop instrucxtions: for() and while() to stop or "jump over" loops.
break
statement stops the execution of a repetitive instruction.<script> //defined to run till x is 10 for(x=0; x<10; x++){ document.write('<br> X is '+x); //stops completely the loop when x is 3 if(x==3) break; } </script>
<script> var x =0; //defined to run till x is 10 while(x <10){ document.write('<br> X este '+x); //stops completely the loop when x is 3 if(x==3) break; x++; } </script>
continue
statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop..<script> //defined to run from 0 to 5 for(x=0; x<5; x++){ //skips the execution when x is 1 or 3 if(x ==1 || x ==3) continue; document.write('<br> X ise '+x); } </script>
<script> var x =0; //defined to run from 0 to 5 while(x <5){ //skips the execution when x is 1 or 3 if(x ==1 || x ==3){ x++; //to increment when it jumps over iteration continue; } document.write('<br> X este '+x); x++; } </script>
label
statements can be used in JavaScript with 'break
' or 'continue
'. It labels a group of nested repetitive instructions, allowing control over that group inside the nested instructions.
label_name: for(...){ //other instructions with for() or while() }- 'label_name' can be any name (except the reserved JavaScript keywords), followed by : and the nested group of for() or while().
<script> loopX: //labels the next set of nested instructions for(var x=0; x<5; x++){ document.write('<h4>X - '+x+'</h4>'); for(var y=0; y<3; y++){ //stops execution of the entire group when x is 2 if(x ==2) break loopX; document.write('<br>Y = '+y); } } </script>
- Testing the code, you will see that though 'break' is inside the second 'for()', by specifying 'loopX', which is the label of the entire set of nested instructions, it will stops the execution of entire group.
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net