How to use the break statement In Javascript
The break Statement comes out of the loop when the condition is true. It breaks out of the loop or switch.
Example: In this example, the break statement is used to exit the for loop when the element “orange” is found in the fruits array. The break statement can be used to exit a loop or switch statement and stop the execution of the script.
Javascript
let fruits = [ "banana" , "apple" , "orange" , "grape" , "strawberry" ]; for (let i = 0; i < fruits.length; i++) { if (fruits[i] === "orange" ) { break ; } console.log(fruits[i]); } |
banana apple
Different Ways to Abort JavaScript Execution
Aborting JavaScript execution can help us to write more robust, secure, and efficient code by preventing unexpected errors and behaviors.