How to exit a function in JavaScript? 3 Effective Ways

In this article, we will delve into different approaches for effectively exit a JavaScript function.

There are times that we have to exit a function while we are still working in a certain condition in JavaScript.

So if you’re asking how to do it, Let’s find out.

What is exit function in JavaScript?

The exit() function is a useful tool in programming languages. It allows you to stop a program or a specific section of code from running.

When you use the exit() function, it immediately ends the program and hands control back to the operating system or the environment where the program is running.

The exit() function comes in handy in different situations. For instance if there’s an error during program execution and you can’t or don’t want to continue running the program.

You can use the exit() function to stop it and let the operating system know about the error.

Solutions on how to exit a function in JavaScript

There is no built-in exit() function in JavaScript like in other programming languages such as C or Python.

However, there are different ways to exit a function early based on certain conditions.

The three ways to exit a function in JavaScript are by using return, break, or throw statements.

Now, let us know and discover the best practices for using return, break, and throw statements to exit functions early and improve your code’s performance.

Solution 1: Use return statement

The return statement in JavaScript serves two purposes: it can be used to exit a function and immediately provide a value, or it can be used to entirely exit the function.

In JavaScript, functions always return something, even if no specific value is mentioned, and by default, this value is undefined.

Here’s the example code using the return statement to exit a function and return a value.

function add(x, y) {
  return x + y;
}
console.log(add(10, 20));

Output:

30

Here’s another example using the return statement to exit a function entirely in JavaScript.

function greet(website) {
  if (website === "Sourcecode") {
    return;
  }
  console.log(`Hi, welcome to ${website}!`);
}
greet("Sourcecode"); // No output
greet("Itsourcecode"); // Output: Hi, welcome to Itsourcecode!

Output:

Hi, welcome to Itsourcecode!

Solution 2: Use break statement

The break statement is mainly used for exiting loops, but it can also be used to exit functions by using labels within the function.

Here’s the example code using the break statement with a label to exit from a function:

function findLargest(numbers) {
  let largest = -Infinity;
  outer: for (let i = 0; i < numbers.length; i++) {
    if (numbers[i] > largest) {
      largest = numbers[i];
      if (largest === Infinity) {
        break outer;
      }
    }
  }
  return largest;
}
console.log(findLargest([10, 20, 30, 40, 50])); // Output: 50
console.log(findLargest([10, 20, Infinity, 40])); // Output: Infinity

In order to find the largest number in an array of numbers, we used a loop to iterate through the elements.

During the iteration, if we encounter the value Infinity, we utilize the break statement along with the outer label to immediately exit both the loop and the entire function.

This approach enables us to cease the search for the largest number as soon as it is discovered.

Output:

50
Infinity

Solution 3: Use the throw statement

To exit a function using the throw statement, you can combine it with a try…catch block.

By throwing an exception, you can exit the function, and then handle the exception in the catch block.

Here’s the example code using the throw statement with a try…catch block to exit a function.

function divide(x, y) {
  if (y === 0) {
    throw new Error("You can't divide any number by zero");
  }
  return x / y;
}
try {
  console.log(divide(10, 2)); // Output: 5
  console.log(divide(10, 0)); // There's no output
} catch (error) {
  console.log(error.message); // You can't divide any number by zero
}

Output:

5
You can't divide any number by zero

Conclusion

In conclusion, this article explores how to exit a function in JavaScript in 3 effective ways that will help you.

Although JavaScript doesn’t have a built-in exit() function like some other programming languages, there are various ways to exit a function based on certain conditions.

You can efficiently exit a function using return, break, or throw statements based on specific conditions.

By using these solutions, you can effectively exit a JavaScript function based on specific conditions, improving code performance and control flow.

We are hoping that this article provides you with enough information that helps you understand the exit function in JavaScript.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Leave a Comment