-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
break
– Exit Loops or Switch Blocks ImmediatelyThe break
statement terminates the loop or switch block where it appears. Once encountered, control jumps to the next line of code after the loop or switch.
break;
for
Loopfor (let i = 1; i <= 5; i++) {
if (i === 3) {
break;
}
console.log(i);
}
// Output: 1 2
Loop stopped as soon as i === 3
.
while
Looplet i = 1;
while (i <= 5) {
if (i === 4) {
break;
}
console.log(i);
i++;
}
// Output: 1 2 3
switch
Statementlet fruit = "banana";
switch (fruit) {
case "apple":
console.log("Apple selected");
break;
case "banana":
console.log("Banana selected");
break;
default:
console.log("Unknown fruit");
}
Without break
, it would continue to execute all cases after a match (fall-through).
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
if (j === 2) break;
console.log(`i=${i}, j=${j}`);
}
}
Only breaks the inner loop, not the outer loop.
Q1. What is the purpose of the break
statement in JavaScript?
Q2. How does break
affect the execution of a loop?
Q3. Write a for
loop that prints numbers from 1 to 10 but stops at 6 using break
.
Q4. Can you use break
in both while
and for
loops? Explain with examples.
Q5. How does break
work inside a switch
statement?
Q6. What happens if you forget break
in a switch
case block?
Q7. Write a program that uses break
to stop looping when a number divisible by 7 is found.
Q8. What is the difference between break
and continue
statements?
Q9. How can you use break
in nested loops to stop only the inner loop?
Q10. Is it possible to break out of multiple nested loops with a single break
? If not, what’s the alternative?
Q1: What does the break statement do?
Q2: Where is the break statement commonly used?
Q3: What happens if you remove break from a switch case?
Q4: Which of the following loops can use break?
Q5: Which statement is true about break in JavaScript?
Q6: How to stop a switch case from falling through to the next case?