JavaScript Break Statement Last Updated : 19 Nov, 2024 Suggest changes Share Like Article Like Report JavaScript break statement is used to terminate the execution of the loop or the switch statement when the condition is true.In Switch Block (To come out of the block) JavaScript const fruit = "Mango"; switch (fruit) { case "Apple": console.log("Apple is healthy."); break; case "Mango": console.log("Mango is delicious."); break; default: console.log("No fruit chosen."); } OutputMango is delicious. Why do we need to use break with Switch?In the below 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."); case "Mango": console.log("Mango is delicious."); break; default: console.log("No fruit chosen."); } OutputApple is healthy. Mango is delicious. In a For Loop (To come out of the Loop) JavaScript for (let i = 1; i < 6; i++) { if (i == 4) break; console.log(i); } Output1 2 3 How does Break Work? While and Do-While Loop Examples 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); Output1 2 3 1 2 3 How does it Work? Break with LabelsIn JavaScript, we can use a break statement with a label to exit from a specific loop, even if it's nested inside another loop. This is useful when you need to break out of a nested loop structure, and just using a simple break would only exit the innermost loop. JavaScript outerLoop: for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (i === 1 && j === 1) { break outerLoop; } console.log(`i: ${i}, j: ${j}`); } } Outputi: 0, j: 0 i: 0, j: 1 i: 0, j: 2 i: 1, j: 0 Create Quiz M mansigeekso9ii Follow 0 Article Tags : JavaScript Web Technologies javascript-basics Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like