How to stop and exit a Function In JavaScript?

In this article we will see how to stop or exit a function in JavaScript.

In programming we come across some situation where we have to exit a function before the whole code block is executed, like for example, in some case, we have to exit the function and return back a value when a certain condition is met.

In JavaScript, to exit or stop a function we can use the return statement or the try catch statement.

Lets see how to use it with examples.

Using return statement to stop a function.

In JavaScript, the return statement ends the execution of a function and return a value to the calling function.

A return statement always return a value. If the value is not specified then it will return as undefined.

We can use the return statement to stop a function before the whole code block is executed. For example

function testFn() {
  console.log('Code Block 1');
  console.log('Code Block 2');
  return;
  console.log('Code Block 3');
  console.log('Code Block 4');
}

console.log(testFn())

Output:

Code Block 1
Code Block 2
undefined

As you can see, after the execution of the first two code block the return statement have stop and exit the function and rest of the code-block was not executed.

Specify Condition to Exit a Function

Using return we can also exit a function when a certain condition is fulfilled.

For example, let try to stop a function when a condition is met using if statement.

function testFn(num) {
  if(num >= 1 ){
    console.log('num is greater then one');
    return;
  }

  console.log('num is smaller then one')
}

console.log(testFn(2))

Output:

num is greater then one

When we use return statement, we do not have to use the else statement to run the next code block.

If the if code-block is not true then the return statement will not run to exit the function, and so the function will eventually run the next code-block.

Exit a function Using try and catch statement

The try..catch statement is usually use to fetch some data and to handle error so that it do not break the code.

The try statement defines the code that you want to run. And the catch statement is where all the error is handle, if you get any in try block.

Syntax:

try {
  // code to run 
  }
catch {
  // code to handle error
}

Here, when an error occurs , JavaScript stops the function and generate an error message. We can use throw statement to create our own custom error message.

In the following example, we will use try… catch along with the throw statement to stop and exit from a function.

function checkNum(num) {
    try {
        if (num < 1) throw "num less then 1";
    } catch (error) {
      console.log('Error Message :',error)
    }
};

console.log(checkNum(0))

Output:

num is less then 1

In the example, the try block is checking if the number is less then 1 or not.

If the number is smaller then 1 it will give us the custom error message “num is less then 1” and the catch block will handle the error and display it to the console.

Conclusion :

To exit a function it is recommended to use the return statement rather then using try…catch statement. The try..catch is generally use when you want to stop a function due to an actual error in the code or due to missing parameter.

Related Topics:

How To Write IsNumber() In JavaScript?

Scroll to Top