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.