The Wayback Machine - https://web.archive.org/web/20240920014713/https://www.geeksforgeeks.org/javascript-break-statement/
Open In App

JavaScript Break Statement

Last Updated : 18 Dec, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JavaScript break statement is used to terminate the execution of the loop or the switch statement when the condition is true.

  • In a switch, code breaks out and the execution of code is stopped. 
  • In a loop, it breaks out to the loop but the code after the loop is executed.

Syntax:

break;

Using Labels

A label reference can be used by the break statement to exit any JavaScript code block. Only a loop or a switch can be used with the break in the absence of a label.

break labelName;

Example 1: In this example, the switch case is executed if the condition is true then it breaks out and the next case is not checked.

Javascript




const fruit = "Mango";
 
switch (fruit) {
    case "Apple":
        console.log("Apple is healthy for our body");
        break;
    case "Mango":
        console.log("Mango is a National fruit of India");
        break;
    default:
        console.log("I don't like fruits.");
}


Output

Mango is a National fruit of India

Example 2: In this example, the fruit name is apple but the given output is for the two cases. This is because of the break statement. In the case of Apple, we are not using a break statement which means the block will run for the next case also till the break statement not appear.

Javascript




const fruit = "Apple";
 
switch (fruit) {
    case "Apple":
        console.log("Apple is healthy for our body");
    case "Mango":
        console.log("Mango is a National fruit of India");
        break;
    default:
        console.log("I don't like fruits.");
}


Output

Apple is healthy for our body
Mango is a National fruit of India

Example 3: In this example, the loop iterate from 1 to 6 when it is equal to 4 then the condition becomes true, and code breaks out to the loop.

Javascript




for (let i = 1; i < 6; i++) {
    if (i == 4) break;
    console.log(i);
}


Output

1
2
3

Example 4: In this example, break statement can is used with while and do-while loop.

Javascript




// Using break in a while loop
let i = 1;
while (i <= 5) {
    console.log(i);
    if (i === 3) {
        break;
    }
    i++;
}
 
// Using break in a do-while loop
let j = 1;
do {
    console.log(j);
    if (j === 3) {
        break;
    }
    j++;
} while (j <= 5);


Output

1
2
3
1
2
3

Supported browsers:

  • Google Chrome
  • Microsoft Edge
  • Firefox
  • Opera
  • Safari


Previous Article
Next Article

Similar Reads

What is the difference between “word-break: break-all” versus “word-wrap: break-word” in CSS ?
The word-break property in CSS is used to specify how a word should be broken or split when reaching the end of a line. The word-wrap property is used to split/break long words and wrap them into the next line. Difference between the "word-break: break-all;" and "word-wrap: break-word;" word-break: break-all; It is used to break the words at any ch
2 min read
What is the use of the Break Statement in a Loop in JavaScript ?
The break statement in JavaScript is used to prematurely terminate a loop, allowing the program to exit the loop before the normal loop condition is met. It is often used to interrupt the loop execution based on certain conditions. Example: Here, the while loop will continue indefinitely (since the condition is true). However, the if statement insi
1 min read
How to Break Statement in an Array Map Method in JavaScript?
Map method in JavaScript is used to create a new array populated with the results of calling a provided function on every element in the calling array. But Unlike loops, the map method cannot be broken once started, and it always iterates through the entire array. Using these approaches we will be break statements in an array map method in JavaScri
2 min read
How to break nested for loop using JavaScript?
The break statement, which is used to exit a loop early. A label can be used with a break to control the flow more precisely. A label is simply an identifier followed by a colon(:) that is applied to a statement or a block of code. Note: there should not be any other statement in between a label name and associated loop. Example-1: Break from neste
3 min read
How to break JavaScript Code into several lines ?
In this article, we will discuss how to break javascript code into several lines. We generally break the code into several lines so that it can be read easily by a third person or the programmer itself. There are a few ways to break JavaScript code into several lines: We can use the backslash \ character i.e "\". We can use template literals.We can
1 min read
JavaScript Program for Word Break Problem
The word break problem is a well-known and fundamental challenge within the field of computer science. The Word Break Problem in JavaScript involves determining if a given string can be segmented into words from a dictionary. It seeks to find a valid combination of dictionary words that compose the entire input string. In this context, a 'word' is
3 min read
JavaScript Break and Continue
Break statementThe break statement is used to jump out of a loop. It can be used to "jump out" of a switch() statement. It breaks the loop and continues executing the code after the loop. Example: This example shows the implementation of the JavaScript Break statement. [GFGTABS] JavaScript let content = &quot;&quot;; let i; for (i = 1; i &lt; 1000;
3 min read
Break the Linked List in Two Halves using JavaScript
A linked list is a linear data structure consisting of nodes, each containing a data element and a reference (or link) to the next node in the sequence. It provides dynamic memory allocation, and easy insertion, and deletion. Unlike arrays, linked lists do not require contiguous memory and allow efficient data manipulation. Its first node is referr
4 min read
How to Add a Line Break in JavaScript?
JavaScript is the behavioural language renowned for its ability to dynamically alter the functionality of graphical user interfaces (GUI). In web development, JavaScript empowers developers to create interactive and responsive web applications by manipulating the Document Object Model (DOM). In this article, we will explore two different approaches
2 min read
Difference Between label and break in JavaScript
The labels and the break statement are used within loops and control flow structures to enhance flexibility and manageability. Each serves distinct purposes and can be applied in the various scenarios based on the needs of the code. These are the following topics that we are going to discuss: Table of Content What is a Label?What is break?Differenc
3 min read
PHP break (Single and Nested Loops)
In PHP break is used to immediately terminate the loop and the program control resumes at the next statement following the loop. Method 1: Given an array the task is to run a loop and display all the values in array and terminate the loop when encounter 5. Examples: Input : array1 = array( 1, 2, 3, 4, 5, 6, 7 ) Output : 1 2 3 4 Loop Terminated The
2 min read
CSS page-break-inside Property
The page-break-inside property in CSS is used to specify how the page breaks inside the element to which it is applied while printing. It inserts a page break or sometimes it is used to avoid a page break inside an element while printing. Syntax: page-break-inside: auto|avoid|initial|inherit Property Values: auto: It is the default value. This valu
2 min read
How to insert line break before an element using CSS?
The white-space property is used to insert the line break before an element. This property controls the text wrapping and white-spacing. Line break between the lines: The line break can be added between the line of text. The white-space: preline; is used to insert line break before an element. Example 1: &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head
2 min read
How to prevent column break within an element?
We can prevent column break within an element by using a CSS break-inside Property. The break-inside property in CSS is used to specify how the column breaks inside the element. Sometimes content of an element is stuck between the column. To prevent column break we should use the break-inside Property set to avoid. Syntax: column-break-inside:avoid
1 min read
How to break _.each() function in Underscore.js ?
It is not possible to break the _.each function. The reason is that the _.each function works similarly to the forEach function and imitates its native behavior. It does not allow us to break the loop or escape from it except by throwing an exception. However, one can use different methods like: Throw Exception _.find() function _.some() function T
2 min read
How to set a single line break in HTML5 ?
In this article, we will add a single line break using the &lt;br&gt; tag. It s used to give the single line break. It is the empty tag so it does not contain end tag. Syntax: &lt;br&gt; Example 1: This example uses &lt;br&gt; tag to break the line. &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; How to defines a single line break?
1 min read
CSS break-inside property
The break-inside property lets you prevent a useless break within a multi-region context, multi-column layout, and in paged media. This property sets how region, column, or page breaks should behave inside a generated box. This property is ignored if there is no generated box. Default Value: auto Syntax: break-inside: Keywor_values; /* Or */ break-
2 min read
How to insert a page break after each footer element in CSS ?
CSS is used for designing a webpage to make it more beautiful and attractive for the users. After inserting a page break after each footer, it will look better and also avoids repetition by using CSS page-break-after property. Syntax: .foot { page-break-after: always; } Page-break-after provides a lot more property values: page-break-after: auto;pa
2 min read
How to apply CSS page-break to print a table with lots of rows?
When printing a table with lots of rows the problem can arise in keeping the data together when the page ends. Since the data presented continuously makes more sense. Here we will track down ways by which one can print contents of a table with lots of rows when the situation of page-break is encountered. The most logical property that can be used f
3 min read
CSS break-after property
The break-after property allows you to place a break on multi-region contexts, paged media, and multi-column layouts. This property describes how the region, column, or page break behaves after the generated box. This property is ignored if there is no generated box at all. Syntax: break-after: Generic break values; /* Or */ break-after: Page break
2 min read
CSS break-before Property
The CSS break-before property is used to mention page break, region break, or column break should occur before the element or not. This property is ignored, if there is no generated box. Syntax: break-before: Generic break values; Or break-before: Page break values; Or break-before: Column break values; Or break-before: Region break values; Or brea
2 min read
How to define word-break property to allow words to be continued to the next line in CSS ?
In this article, we will see how to define a word-break property to allow words to be continued to the next line in CSS. You can use break-all and break-word property values to define the word-break property which is used to specify how to break the word when the word reached at end of the line. Syntax: word-break: break-all | break-word; Property
2 min read
How to break forEach() method in Lodash ?
The Lodash _.forEach() method iterates over elements of the collection and invokes iterate for each element. In this article, we will see how to break the forEach loop in ladash library. Syntax: _.forEach( collection, [iterate = _.identity] ) Parameters: This method accepts two parameters as mentioned above and described below: collection: This par
2 min read
Primer CSS Timeline TimelineItem-Break
Primer CSS is open-source on Github that is created with GitHub’s design system. We can use Primer CSS by installing it via npm or include the CDN link in our HTML file. It has different types of styles like spacing, color, and typography. Primer CSS Timeline is used to display the items in the vertical direction. Primer CSS TimelineItem-Break is u
3 min read
Primer CSS Typography Word-Break
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by object-oriented CSS principles, functional CSS, an
2 min read
How to wrap or break long text/word in a fixed width span ?
In this article, we will see how to wrap long text/word in a fixed-width span. The word-wrap property in CSS is used to break long words and wrap them into the next line. It defines whether to break words when the content exceeds the boundaries of its container. Syntax: word-wrap: normal|break-word|initial|inherit; Example 1: The following code dem
1 min read
Bootstrap 5 Text Word break
Text Word break is used to break the long string. Prevent long strings of text from breaking your components' layout by using text-break. This class is deprecated because a line break is not possible in Arabic, and the most used RTL language is Arabic. Bootstrap 5 Text Word break Class: text-break: This class is used to break the line. Syntax: &lt;
2 min read
Short Circuit Array.forEach Like Calling Break
The Array.forEach() loop is a built-in method in JavaScript that is used to reiterate over any array and execute a function for each element of the array. The main purpose of this method is to perform some operation on each element of the array without having to manually iterate over it using a loop. The Array.forEach() method is used to execute a
3 min read
What is the purpose of using the CSS box-decoration-break Property ?
The box-decoration-break Property in CSS is used to control the rendering behavior of box decorations, such as borders and backgrounds when a box is broken across multiple lines. It allows you to determine whether these decorations should behave as continuous or broken across line breaks. Note: The box-decoration-break Property accepts values like
1 min read
How to Add a Line Break with the br Tag in HTML?
The &lt;br&gt; tag in HTML is a simple yet essential element used to insert a line break within a block of text or content. In this article, we'll explore the usage of the &lt;br&gt; tag and discuss various scenarios where it can be effectively applied. Usage of the &lt;br&gt; Tag The &lt;br&gt; tag, also known as the line break tag, is a self-clos
2 min read