Various Ways to Terminate JavaScript Execution

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

function checkNumber( num) {

if ( isNaN( num)) {

return " Not a number!";

}

return num * 2;

}

console.log( checkNumber( " abc" ) );

console.log( checkNumber( 7) );

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

let fruits = [ "banana", "apple", "orange", "grape", "strawberry" ];

for ( let i = 0; i < < fruits.length; i++) {

if ( fruits[i] === " orange") {

break;

}

console.log( fruits[i]);

}

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

let numbers = [ 1, 2, 3, 4, 5 ];

for ( let i = 0; i < < numbers.length; i++) {

if ( numbers[i] % 2 === 0) {

continue;

}

console.log( numbers[i]);

}

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

function checkAge( age) {

if ( age < < 18) {

toss " Need to be 18 or older";

}

console.log( " You are permitted to go into" );

}

attempt {

checkAge( 17 );

} catch ( mistake) {

console.log( mistake );

}

Output
 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

<

<<

< Stop Packing Example<<

< < Packing Example< < This page is taking a long period of time to load ...

< Stop Loading<

< const stopBtn = document.getElementById(' stop-btn');

stopBtn.addEventListener(' click', () => > { window.stop(); } );

<< <

In the above example, when the user clicks the "Stop Loading" button, the window.stop() function is called, which right away stops the packing procedure and shows whatever material has actually currently been packed on the page. This can be beneficial for enhancing the user experience on slow-loading pages. The clearTimeout() and clearInterval techniques in javascript clear the timeout which has actually been set by the setTimeout() and setInterval operates prior to that. Example: Javascript let timeoutId = setTimeout(

function() {

console.log(" This message will be logged after 3 seconds");

}, 3000);

clearTimeout( timeoutId);

let count = 0; let intervalId = setInterval(

function() { count++;

console.log(" Count is now:"

, count);}, 1000);

setTimeout(

function() {

clearInterval( intervalId); console.log(

" Period has actually been cleared"

);

}, 5000); Output Count is now: 1 . Count is now: 2
.
Count is now: 3 .
Count is now: 4 . Period has actually been cleared

In the above example, we initially utilize setTimeout to perform a function after 3 seconds. Nevertheless, we right away cancel the timeout utilizing clearTimeout

, so no message will be logged to the console.

Next, we utilize setInterval

to perform a function every 2nd, which increments a

count variable and logs its worth to the console. After 5 seconds have actually passed, we utilize

clearInterval

to cancel the period and log a message showing that the period has actually been cleared. As an outcome, we see the count boost to 5 prior to the period is cleared, and after that the message "Period has actually been cleared" is logged to the console. Conclusion: Therefore, selecting the suitable approach to

terminate JavaScript execution depends upon the particular usage case and what you wish to attain. You must thoroughly think about the ramifications of each approach and pick the one that finest fits your requirements.

Last Upgraded: 24 Jul, 2023 Like Post Conserve Post

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: