Skip to content
Related Articles
Open in App
Not now

Related Articles

How to terminate a script in JavaScript ?

Improve Article
Save Article
  • Last Updated : 25 Jan, 2023
Improve Article
Save Article

In this article, we will discuss the different ways to terminate the script in JavaScript. We have a few methods to terminate a script in JavaScript that are described below:

Method 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: 

javascript




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

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




<script>
    function doSomeThing() {
        var i = 10;
        if (i === 10)
            return;
      
        var 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');
</script>

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




<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];
      
        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);
    }
</script>

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




<script>
    function doSomeThing() {
        var i = 10;
        console.log('Terminating ');
        process.exit(0);
        var 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();
</script>

Output:

Terminating:

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!