
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to break a loop in JavaScript?
The break statement is used to break a loop and continue executing the code, which is after the loop.
Example
You can try to run the following code to break a loop in JavaScript
<!DOCTYPE html> <html> <body> <p id="test"></p> <script> var text = ""; var i; for (i = 0; i < 5; i++) { if (i === 2) { break; } text += "Value: " + i + "<br>"; } document.getElementById("test").innerHTML = text; </script> </body> </html>
Output
Value: 0 Value: 1
- Related Articles
- How to break a for loop in Python?
- How to use the break statement to come out of a loop in JavaScript?
- How to break out of a loop in Perl?
- Bash break How to Exit From a Loop
- How to use PowerShell break statement in foreach loop?
- How do we use a break statement in while loop in C#?
- How to use PowerShell Break statement with the While Loop?
- How to use PowerShell break statement with the For loop?
- How to control for loop using break and continue statements in C#?
- break, continue and label in Java loop
- How to create a line break with JavaScript?
- How can I break an outer loop with PHP?
- Finding how many time a specific letter is appearing in the sentence using for loop, break, and continue - JavaScript
- How do you create a Tkinter GUI stop button to break an infinite loop?
- How to add delay in a loop in JavaScript?

Advertisements