Terminating JavaScript execution can assist us to compose more robust, protected, and effective code by avoiding unforeseen mistakes and habits.
Possible reasons that we require to terminate JavaScript execution are:
- We may require to terminate execution when a mistake takes place to avoid additional execution of the code and prevent unforeseen habits or information corruption.
- We may require to terminate execution when user input is void to avoid the code from keeping up inaccurate or unforeseen input.
- We may require to terminate execution in the middle of a loop if a particular condition is satisfied, or if we have actually discovered the outcome we were searching for.
- We may require to terminate execution in many cases to enhance efficiency, such as when utilizing setInterval or setTimeout, where we may require to stop the execution of a function or loop after a particular quantity of time.
- In many cases, it may be essential to terminate execution to avoid harmful code from running, or to avoid delicate information from being accessed.
Various methods to terminate JavaScript execution are as follows:
Utilizing the return declaration
Return declarations in a shows language are utilized to avoid the presently performing function and go back to the caller function
Example:
Javascript
|
In this example, the return declaration is utilized to terminate the function checkNumber if the input is not a number, and return the message “Not a number!”.
The break Declaration comes out of the loop when the condition holds true. It breaks out of the loop or switch.
Example:
Javascript
|
In this example, the break declaration is utilized to leave the for loop when the aspect "orange" is discovered in the fruits selection. The break declaration can be utilized to leave a loop or switch declaration and stop the execution of the script.
The continue declaration in Javascript is utilized to break the version of the loop and follows with the next version. The break in the version is possible just when the defined condition going to happen.
Example:
Javascript
|
In this example, the continue declaration is utilized to avoid the even numbers and print just the odd numbers in the numbers selection. The continue declaration can be utilized to avoid to the next version of a loop and terminate the existing version.
Utilizing the toss declaration
The toss declaration lets you make your own mistakes
Example:
Javascript
|
Should be 18 or older
In this example, the toss declaration is utilized to toss a mistake and stop the execution of the function checkAge if the input age is less than 18.
Utilizing the window.stop Technique
The stop() approach in DOM is utilized to stop the window from packing resources in the existing searching context, comparable to the internet browser's stop button
Example: We have a website with some material that takes a long period of time to load, and you wish to offer the user the capability to stop the packing procedure if they get tired of waiting. You can include a "Stop" button to your page and connect a click occasion listener to it. Inside the occasion listener function, you can call the window.stop() function to right away stop the packing procedure.
HTML
|
function() {
clearInterval( intervalId); console.log(
" Period has actually been cleared"
);
|