How to exit a function in javascript?

In this article, we are going to see the different types of javascript exit functions.

If you are new to programming or JavaScript, we recommend you read through the entire article. However, If you are just looking for the code, you can skip to the code section below.

We may often come across the situation of wanting to exit a function early, for example in the case of building functions with multiple conditions, we may want to exit the loop if a specific condition is satisfied. In languages such as PHP and C, an exit() function exists which can be used to exit the loop in such cases.

So, how do we exit functions in javascript?

There are 3 major javascript exit functions namely, return, break, or throw. We will discuss these in detail below.

One point to remember is that functions in javascript always return something even if not stated explicitly, with undefined being the default return value.

const getName = (name) => {

  if (name == 'Flexiple') {

    console.log(name);
   }
}

// This function actually returns undefined

Table of Contents


Using return to exit a function in javascript

Using return is the easiest way to exit a function. You can use return by itself or even return a value.

Code:

//javascript exit function using return
function add(a,b){

// if a and b is empty then exit the function
    if(!a && !b ){
        return;
    }

   return a+b;
}


console.log(add(1,3));
// 4

console.log(add());
// undefined

Code Explanation:

In this function, we first check if 'a and 'b' are empty, in which case we exit the function using return, otherwise we return the sum of a and b.

Note:

Any code you add after the return won't get executed.

function something(arr){
  return arr;

  // it doesn't execute.
  console.log('not executed');
}

Using break to exit a function in javascript

Using break to exit from functions in javascript is a less traditional way compared to using return. Break is mostly used to exit from loops but can also be used to exit from functions by using labels within the function.

//javascript exit function using break

const getName = () => {

  getName : {

    console.log("I get logged");

    break getName ;

    //exits the function

    console.log("I don't get logged");
  }
};

Using throw to exit a function in javascript

While try and catch are usually used for fetching data, it can also be used to exit a function.

//javascript exit function using throw

const getName = (name) => {

  try {

    //get out of here

    if (name === "flexiple") throw "exit";

    //exits the function if name is flexiple
  }

 catch (e) {

    // handle exception

  }
};

Code Explanation

  • In this function we first check if the name is flexiple, in which case we throw an exception that is then handled in the catch block.
  • Since there is no piece of code in catch block, it will simply exit the loop - which is what we intended to achieve in the first place.
  • This is yet another unconventional way to exit a function in javascript.

Browser Support

All the three javascript exit functions, throw, break, and return are fully supported by all the browsers.