How to terminate a script in JavaScript ?

14 Jul, 2020
In this article, we will discuss the different ways to terminate the script in JavaScript.
1. Using return: In order to terminate a section of the script, a return statement can be included within that specific scope.
Terminating the entire Script:<script>    var i = 10;      // Return statement is in     // the global scope    if (i === 10)        return;              var 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 }));</script>
<script>    var i = 10;      // Return statement is in     // the global scope    if (i === 10)        return;              var 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 }));</script>
Outputs:
In the absence of return statement:This section will not be executed
3, 4, 5, 6, 7, 8, 9
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 encounter.
No output will be shown since the program is terminated on encounter.
2. Terminating a part of the Script:
<script>    function doSomeThing() {        var i = 10;        if (i === 10)            return;          var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];          document.write(            'This section will not be executed');        document.write(arr.filter(            (elem, index) => { return elem > 2 }));    }      let i = doSomeThing();    if (i === undefined)        document.write('Terminated');</script>
Output:
Terminated
Explanation: 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.
3. Throwing an error on purpose:
<script>    function doSomeThing() {        var i = 10;        if (i === 10)            throw new Error(                'Program Terminated');          var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];          document.write(            'this section will not be executed');                      document.write(arr.filter(            (elem, index) => { return elem > 2 }));    }      try {        doSomeThing();    }    catch (err) {        document.write(err.message);    }
Output:
Program Terminated

Explanation: 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.
4. Using process.exit for Node.JS applications:
<script>    function doSomeThing() {        var i = 10;        document.write('Terminating ');        process.exit(0);        var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];          document.write(            'This section will not be executed');                      document.write(arr.filter(            (elem, index) => { return elem > 2 }));    }    doSomeThing();</script>
Output:
Terminating:
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 the cases.
JavaScript-Misc
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
Roadmap to Learn JavaScript For Beginners
JavaScript | Promises
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?