What is break statement in javascript
In this article, we will see about what is break statement in javascript.
What is break statement
Most important thing you remember is break statement is terminates the loop. Browser skills statements under the break statement, and transfer the control to the end of the loop nearest or switch case. Break statement is used inside switch and looping statement.
Syntax:
break;
Example code
for(var i=1; i<=5; i++){
if(i==3){
break;
}
document.write("Hello World <br/>");
}
Output:
In the above code, we created the for loop with starting point is i=1. We give a condition i<=5, if this condition becomes false then the loop terminates. Inside the loop, we check “if condition” (i==3), if the condition is true the loop will go inside and we give “break:” so, the loop will be terminate, else printing the document statement “Hello World”.
When the loop repeats for the 3rd time, the if condition becomes true, so the loop is terminated, because we give “break;” statement inside the if condition. So, the “Hello World” is printed twice.