Skip to content
Related Articles

Related Articles

How to terminate a script in JavaScript ?
  • Last Updated : 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>

    Outputs:

  • In the absence of 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 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.

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :