Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

Break statement in JavaScript

The JavaScript break statement breaks out or exits the current loop, switch statement , or a labeled block. It is a very useful statement, which helps us to exit the loop midway when a certain condition occurs. The break transfers the control to the very next statement after the loop, switch, or a labeled block.

Syntax

The Syntax of the break statement is as follows.

                            
 break [label];
  
                        
                            
                        

Where the label is optional. Use it to correctly identify which loop to exit in the case of a nested loop. It is a must if you want to exit out of a labeled block

Break out of a for Loop

In the following example, the if statement breaks out when the value of i is 6. The values 6 to 10 are never printed as the for loop terminates after the break.

                            
for (let i = 0; i < 10; i++) {
  if (i == 6) break;
  console.log(i);
}
 
 
*** Console ***
0
1
2
3
4
5
 

 
 
                            
                        
                            
                        

Break out of a while Loop

Similarly, we break out here when the value of num2 is 3.

                            
let num2 = 0;
while (num2 < 6) {
  if (num2 === 3) {
    break;
  }
  num2 = num2 + 1;
}
console.log(num2);
 

 
 
                            
                        
                            
                        

Break out of a switch

The Switch statements use the Break to break out of the switch. Otherwise, the execution of the switch continues to the next case clause.

                            
//Exapmple 1
 
let val1 = 20;
let val2 = 10;
let operation = "-";
 
console.log("switch example");
 
switch (operation) {
  case "+":
    console.log(val1 + val2);
    break;
  case "-":
    console.log(val1 - val2);
    break;
  case "*":
    console.log(val1 * val2);
    break;
  case "/":
    console.log(val1 / val2);
    break;
  default:
    console.log("Invalid operator");
}
 
console.log("switch finished");
 
                            
                        
                            
                        

Break out of a nested Loop

In the following code, we have a nested for loop.

The break statement here exits the inner loop and not from the outer loop.

                            
let i = 0;
let j = 0;
let sum = 0;
 
for (i = 0; i < 10; i++) {
 
  for (j = 0; j < 10; j++) {
    if (i == 6) break;
  }
 
}
 
console.log(i); //10
console.log(j); //10
 
 
                            
                        
                            
                        

In the following example, we prefix the outer loop with outerloop label. And inner loop with innerloop label.

In the break statement, we use the outerloop, which will make the break to exit from the outer loop

                            
outerloop: for (i = 0; i < 10; i++) {
 
  innerloop: for (j = 0; j < 10; j++) {
    if (i == 6) break outerloop;
  }
}
 
 
console.log(i); //6
console.log(j); //0
 
 
 
                            
                        
                            
                        

Break from a labeled block

In the following example, we have two blocks outer & inner. The statement break outer; will exit from the outer block.

                            
let count = 1;
outer: {
 
  inner: {
    console.log(count);
 
    break outer;
 
    count++;
    console.log(count);
  }
 
  count++;
  console.log(count);
}
 
console.log(count); //1
 
 
 
                            
                        
                            
                        

The following results in a syntax error. The break statement can only exit out of a block, loop, or a switch, which it is part of.

In the following example, the break cannot exit blk2, because it is not part of it. It can only exit from the blk1. Hence the following code results in an error.

                            
let z = 0;
blk1: {
  console.log(z);
  break blk2;
}
blk2: {
  console.log(z);
}
console.log(z); //1