Break Statement in JavaScript : Terminating Loop in JS

February 1, 2025 No Comments » Hits : 434





Continue Statement in JavaScript : Terminating Loop in JS

Break Statement Simply Terminate Loop and takes control out of the loop.

Break in For Loop :

for(initialization ; condition ; incrementation)
{
   Statement1;
   Statement2;
   break;
}

Break in While Loop :

initialization ; 
while(condition)
{
   Statement1;
   Statement2;
   incrementation
   break;
}

Break Statement in Do-While :

initialization ; 
do
   {
    Statement1;
    Statement2;
    incrementation
    break;
}while(condition);

Way 1 : Do-While Loop


Way 2 : Nested for


Way 3 : For Loop


Way 4 : While Loop

Live Example :

<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=10;i++)
{
if (i==3)
  {
  break;
  }
document.write("The number is " + i);
document.write("<br />");
}
</script>
<p>Explanation: The loop will break when i=3.</p>
</body>
</html>

Output :

The number is 0
The number is 1
The number is 2
Explanation: The loop will break when i=3.

Incoming search terms: