How to terminate a script in JavaScript?

The termination of the script means that it stops executing the JavaScript code. In some emergency cases, developers requires to abort the execution of JavaScript code in the middle while the script is executing.

Also, we can use the if-else statement to decide when to terminate the script execution and when to continue. Here, we will learn different ways to terminate the script midway.

Use the Return Statement

The return statement is used to terminate the execution of any code inside the script. Once we execute the return statement inside the function, the code written after the return statement will not execute. However, we don’t need to return any value with the return statement, as we can just use the return keyword.

Syntax

Users can follow the syntax below to use the return statement to terminate the execution of the script in JavaScript.

function execute() {
   
   // this code will be executed
   return;
   
   // this code will not be executed.
}

In the above syntax, we have used the return statement with the function.

Throw an error to terminate a script

We can throw custom errors using the throw keyword. We can use the Error() constructor to create a new error. we throw an error from anywhere inside the script to stop execution. When we throw an error, it will not execute statements written after the throw statements.

Syntax

Users can follow the syntax below to throw an error to terminate the execution of the script in JavaScript.

throw new Error("Error_Message"); 

In the above syntax, ‘Error_message’ is a message of the error to show to users.

Use the clearInterval() method

The clearInterval() method takes the id of the timer as a parameter to clear the timer. We can set the timer to execute any function. For example, we can execute some scripts after some delay using the setTimeOut() method. If we require to stop the execution of the script, we can clear timeout using the clearInterval() method before the script executes.

Syntax

Users can follow the syntax below to use the clearInterval() method to terminate the execution of the script.

let timeVar = setTimeout(() => {
   
   // stop execution of this script
},delay);
clearInterval(timeVar);

In the above syntax, we can stop the execution of the script written inside the callback function of the setTimeOut() method.

Use the process.exit() method in Node.js

The process.exit() will not work with vanilla JavaScript, and it will only work with Node.js as we need to import the ‘process’ module. We can execute the process.exit() method by passing 0 as a parameter to terminate the script.

Syntax

Users can follow the syntax below to use the process.exit() method to terminate the script.

process.exit(0); 

In the above syntax, we have passed 0 as a parameter for the termination purpose.

Conclusion

We learned various approaches to terminate the script in JavaScript. The first way is using the return statement, the second is by throwing an error, the third is using the clearInterval() method, and the last is using the process.exit() method.

Related Posts

What are custom events in JavaScript?

Custom events are the events that allow you to decouple the code you want to run after a specific piece of code runs. There are various in-built events…

How to use nested for loop in JavaScript?

We use the for loop statement of JavaScript for repeating a set of statements inside the loop body a specified number of times. A nested for loop, as the…

What are the basic rules for JavaScript parameters?

A JavaScript function is a code that performs a particular task. The function parameters are the name list in the function definition. Parameters are also known as…

How to stop refreshing the page on submit in JavaScript?

Using event.preventDefault() to stop page refresh on form submit In this section, we will see how to use event.preventDefault() to stop page refresh on form submission. The event.preventDefault() restricts the default…

Target a Window Using JavaScript or HTML

TARGET attribute of HTML A link’s opening named frame or window is specified using the Target attribute of the <a> anchor tag. The concluding </a> tag in…

What is the role of deferred scripts in JavaScript?

Since JavaScript is a loosely typed language, you are not required to correctly predict the kind of data that will be kept in a variable. Depending on the information…