How to terminate a script in JavaScript ?
To terminate a script in JavaScript, we have different approaches:
Table of Content
- Using return
- Terminating a part of the Script
- Throwing an error on purpose
- Using process.exit for Node.JS applications
- Using clearInterval() method
Method 1: Using return
In order to terminate a section of the script, a return statement can be included within that specific scope.
Example: Here, we are using return statement.
javascript
let i = 10; // Return statement is in // the global scope if (i === 10) return ;let i = 10; // Return statement is in // the global scope if (i === 10) return ; let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; document.write( 'This section will not be executed' ); document.write( '<br>' ); document.write(arr.filter( (elem, index) => { return elem > 2 })); let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; document.write( 'This section will not be executed' ); document.write( '<br>' ); document.write(arr.filter( (elem, index) => { return elem > 2 })); |
Output:
In the absence of the return statement:
This section will not be executed
3, 4, 5, 6, 7, 8, 9
In the presence of return:
No output will be shown since the program is terminated on the encounter.
Method 2: Terminating a part of the Script
Example: Depending on a condition, a certain section of the script can be terminated using a “return” statement. The “return ” statement returns “undefined” to the immediate parent scope, where the termination can be handled. This is the best practice since handling terminations makes it easier for future debugging and readability of the code.
javascript
function doSomeThing() { let i = 10; if (i === 10) return ; let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; console.log( 'This section will not be executed' ); console.log(arr.filter( (elem, index) => { return elem > 2 })); } let i = doSomeThing(); if (i === undefined) console.log( 'Terminated' ); |
Output:
Terminated
Method 3: Throwing an error on purpose
Example: We deliberately throw an error to terminate the section of the script that we want to. It is best practice to handle the error using a “try-catch” block.
javascript
function doSomeThing() { let i = 10; if (i === 10) throw new Error( 'Program Terminated' ); let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; console.log( 'this section will not be executed' ); console.log(arr.filter( (elem, index) => { return elem > 2 })); } try { doSomeThing(); } catch (err) { console.log(err.message); } |
Output:
Program Terminated
Method 4: Using process.exit for Node.JS applications
Example: There will be no other output, in this case since we exited the script. This is applicable to all JavaScript applications in the NodeJS environment. There are numerous other methods such as process.kill(process.pid) and process.abort() in NodeJS but process.exit() suffices for most of the cases, if not all cases.
javascript
function doSomeThing() { let i = 10; console.log( 'Terminating ' ); process.exit(0); let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; console.log( 'This section will not be executed' ); console.log(arr.filter( (elem, index) => { return elem > 2 })); } doSomeThing(); |
Output:
Terminating:
Method 5: Using clearInterval() method
The clearInterval() function in javascript clears the interval which has been set by the setInterval() function before that.
Syntax:
clearInterval(nameOfInterval);
Example: Here, Clicking the “Start Script” button initiates a repeating action every second using setInterval()
. Clicking the “Stop Script” button stops the repeating action by calling clearInterval(intervalId)
, where intervalId
is the ID returned by setInterval()
. The script logs “Repeating action…” to the console every second until you click “Stop Script,” at which point it logs “Script stopped.”
HTML
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < meta http-equiv = "X-UA-Compatible" content = "IE=edge" > < meta name = "viewport" content = "width=device-width, initial-scale=1.0" > < title >Script Termination Example</ title > </ head > < body > < button id = "startButton" >Start Script</ button > < button id = "stopButton" >Stop Script</ button > < script > let intervalId; // Variable to store the interval ID function repeatingAction() { console.log("Repeating action..."); } document.getElementById("startButton") .addEventListener("click", function () { // Start the repeating action every 1 second intervalId = setInterval(repeatingAction, 1000); }); document.getElementById("stopButton") .addEventListener("click", function () { // Stop the repeating action when the "Stop Script" // button is clicked clearInterval(intervalId); console.log("Script stopped."); }); </ script > </ body > </ html > |
Output:
Contact Us