Home / JavaScript / JavaScript break And continue Statements For Loops
JavaScript provides commands through which iterations of a loop can be skipped or stopped. These commands are most commonly used along with an if statement inside the loop.
Thus, if you ever have a need to stop a loop – while, for – simply use these.
For example, continue can be employed to display only the even numbers between 1 to 20, skipping the odd numbers.
var msg = ""; for (var x = 0; x <=20; x++) { if (x%2) { continue; } msg = msg + x + "\n"; } alert(msg);
Click here to check the results
The condition in if checks for a remainder when variable x is divided by 2. Thus, for odd numbers, the condition will be ‘true’ and the loop will be skipped because of continue.
Similarly, break is employed to stop loop iterations completely.
var msg = ""; var t = 1; while (t <= 10) { if (t == 8) { break; } msg = msg + t + "\n"; t++; } alert(msg);
Click here to check the results
Loop iteration stops when the value of variable t equals 8.
How do I view a deleted web page?
Is there a way to view a deleted web page - one that is no longer available? Yes there is and the solution is quite simple. [more...]
Wingdings included since Windows 3.1 has been the center of a strange controversy. The dingbat font contains many popular and common shapes that can quickly be incorporated in documents. In the original font, typing the three letters 'NYC' in sequence, would display skull-cross bones, star of David and thumbs up sign. Some people interpreted as an antisemitic message and raised a big hue and cry over it. In later versions, the symbols were changed to eye, heart and skyline, which is like a hat tip to the famous "I ♥ NY" logo designed by Milton Glaser. [more...]
We use cookies to give you the best possible website experience. By using WebDevelopersNotes.com, you agree to our Privacy Policy