×

Index

JavaScript Tutorial

JavaScript Examples

CSS Tutorial

CSS Examples

break statement with example in JavaScript

JavaScript break statement: Here, we are going to learn about the break statement in JavaScript with example.
Submitted by IncludeHelp, on March 11, 2019

JavaScript break statement

break is a keyword in JavaScript, it is used with the switch statement and looping to exist the control from the switch or loops block.

When a break statement executes either in a switch statement or in a loop statement, it breakouts the execution and jumps to the next statement written after the block of switch or loop.

Syntax

    break;

Example of break statement with switch statement

<html>
<head><title>JavaScipt Example</title></head>

<body>
<script>
	var val = 2;
	switch(val){
		case 1:
			document.write("One<br>");
			break;
		case 2:
			document.write("Two<br>");
			break;			
		case 3:
			document.write("Three<br>");
			break;			
		default:
			document.write("NOne<br>");
			break;			
	};
</script>
</body>
</html>

Output

Two

Example of break statement with loop statement

<html>
<head><title>JavaScipt Example</title></head>

<body>
<script>
	var loop = 1;
	//break when value of loop is 7
	for(loop=1; loop<=10; loop++){
		if(loop==7)
			break;
		document.write(loop + " ");
	}
	document.write("<br>");
	document.write("Outside of the loop<br>");
</script>
</body>
</html>

Output

1 2 3 4 5 6 
Outside of the loop

JavaScript Tutorial »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.