Learn JavaScript

JavaScript break

To manipulate, JavaScript loops and switch statements / blocks, JavaScript provides the mechanism called JavaScript break. Sometimes in a program, it may happen that we do not need to execute all the statements inside the loop statement or we do not need to perform all iterations. In such cases JavaScript break allows the execution, to come out of the loop.

While learning JavaScript switch cases, we briefly discussed the JavaScript break with switch cases. JavaScript break is normally used to come out of the loop.

Standard Syntax of JavaScript break

Syntax 1

loop (loop statements)
{
Statements;
break;
}

Syntax 2

switch (expression)
{
case 1: Set of statements;
break;
case 2: Set of statements;
break;
.
.
.
case n: Set of statements;
break;
default: set of statements;
}

Example: JavaScript program to write first 5 even numbers, using JavaScript break.

<html>
<body>
<h2>Program to understand the working of break</h2>
<h2>JavaScript program to write first 5 even numbers</h2>
<script>
var i = 0;
while (i <= 15)
{
if (i==10)
{
break;
}
i+=2;
document.write(i);
}
</script>
</body>
</html>

Output

Program to understand the working of break

JavaScript program to write first 5 even numbers

2
4
6
8
10


Click Here to Learn – JavaScript break with switch case.

Audience

This JavaScript tutorial is designed to help the beginners as well as the professional developers. For better understanding the language of the tutorial is kept simple and easy to understand.

Prerequisites

This tutorial demands to have the basic knowledge of the programming languages such as c or c++, HTML, loops for the better understanding of the JavaScript programming language. The other basic requirement to learn JavaScript through this tutorial is the basic knowledge of computer and information technology so you can easily acquire a better understanding JavaScript programming.

Community Helping

In case you find any kind of error like typing error, spelling mistakes, incorrect grammar, incorrect information or any other mistakes. Please feel free to write to us at: akyadav@etutororg.com.