JavaScript break loop

for, while, and do while loops can be exited at any time using the break keyword.

Example 1: exit for loop
<html>
<head>
<title>break for loop</title>
<script type="text/javascript">
for(var i=0;i<100;i++)
{
document.write(i+"<br>");
if(i==50) break;
}

</script>

</head>
<body>

</body>
</html>

Example 2: exit while loop

<html>
<head>
<title>break while loop</title>
<script type="text/javascript">
var i=0;
while(i<=100){
document.write(i+"<br>");
if(i==50) break;
i++;
}

</script>

</head>
<body>

</body>
</html>

Posted by: Dara | post date: 03-23-2013 | Subject: JavaScript




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.