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 require 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 function. 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.

Example

In the example below, we invoke the execute() function whenever the document loads on the web page. Inside the execute() function, we check if the first value of the array exists and continue execution; otherwise, we execute the return statement to stop the execution of the script.

<html>
<body>
   <h3>Using the <i> return statement </i> to terminate the script in JavaScript</h3>
   <div id = "content"> </div>
   <script>
      let content = document.getElementById("content");
      let array = [];
      function execute() {
         content.innerHTML = "This is a JavaScript code.";
         if (!array[0]) {
            return;
         }
         content.innerHTML = "This statement will not execute!";
      }
      execute();
   </script>
</body>
</html>
This is a JavaScript code.

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 can 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.

Example

In the example below, we have thrown the error using the throw keyword from the execute() function. Also, we have triggered the function call inside the try-catch block to handle the errors. Users can observe in the output that the script will stop the execution after we throw an error.

<html>
<body>
   <h3>Throwing the <i> error </i> to terminate the script in JavaScript.</h3>
   <div id = "content"> </div>
   <script>
      let content = document.getElementById("content");
      function execute() {
         throw new Error("This is an error to stop execution!");
         content.innerHTML += "This statement will not execute!";
      }
      try {
         execute();
      }
      catch (err) {
         content.innerHTML += "Error: " + err.message;
      }
   </script>
</body>
</html>
Error: This is an error to stop execution!

Use the clearTimeout() Method

The clearTimeout() method takes the id of the timer as a parameter to clear the timer. We can set the timer to execute any function using the setTimeout() method. If we require to stop the execution of the scheduled script, we can clear timeout using the clearTimeout() method before the script executes.

Syntax

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

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

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

Example

In the example below, we used the setTimeout() method to execute the script after a delay of 2000 milliseconds. We clear the timer using the clearTimeout() method before the script executes, and that is how we can stop the execution of any scheduled script in JavaScript.

<html>
<body>
   <h3>Using the <i> clearTimeout() method </i> to terminate the script in JavaScript.</h3>
   <div id = "content"> </div>
   <script>
      let content = document.getElementById("content");
      let timeVar = setTimeout(() => {
         content.innerHTML = "This is inside the setTimeout() function!";
      }, 2000);
      content.innerHTML = "This is outside the setTimeout() function!";
      clearTimeout(timeVar);
   </script>
</body>
</html>
This is outside the setTimeout() function!

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

The process.exit() method 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 entire Node.js process.

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 successful termination purpose.

Example

In the example below, we have written Node.js code. We have imported the process module in the code. We have assigned 30 to the 'num' variable. The if statement condition always evaluates true, so it will stop the execution of the code, which we can observe in the output.

// Importing process module
var process = require('process');
let num = 30;
console.log("The value of number is " + num);
if(num > 20) {
   process.exit(0);
}
console.log("This line will not be printed as process.exit() is called");
The value of number is 30

Comparison Table

Method Scope Use Case Environment
return Function only Exit function execution Browser/Node.js
throw Error Script/Function Stop with error handling Browser/Node.js
clearTimeout() Scheduled code Cancel delayed execution Browser/Node.js
process.exit() Entire process Terminate Node.js application Node.js only

Conclusion

We learned various approaches to terminate script execution in JavaScript. Using the return statement is best for function-level termination, while throwing errors provides controlled script stopping with error handling. The clearTimeout() method cancels scheduled execution, and process.exit() terminates the entire Node.js process.

Updated on: 2026-03-15T23:19:01+05:30

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements