JavaScript Exit Function

JavaScript Exit Function

In JavaScript, the exit function is used in two conditions. The first is when you achieve your desired output and don’t continue the running program. The second is when you want to exit quickly without receiving your desired result.

On the other hand, exiting a JavaScript function is useful when we need to make a speedy exit in the middle of the function or when a certain condition is met.

Additionally, it helps in some situations where you only want to use a portion of the function’s functionality in your application.

You may learn how to terminate a JavaScript function by reading this article.

In this article, we discuss the “JavaScript Exit Function,” Mainly, we have used the return statement for exiting in a JavaScript function, but two more methods which help to exit from a JavaScript function that is broken and Flow statement methods.

Let’s start to discuss this.

What is an exit function?

An exit function in JavaScript allows a programmer to exit a function or a loop prematurely based on certain conditions. The purpose of an exit function is to stop the execution of the current code block and immediately return a value or exit the function entirely.

There are different types of exit functions in JavaScript, such as the “return” statement, the “break” statement, and the “throw” statement. These statements allow you to exit a function, a loop, or a switch statement before reaching the end of the block.

Using an exit function in your code can help you prevent unintended behavior and errors. It makes your code more readable and easier to maintain and ensures. It executes only once. It is an important tool for controlling program flow and ensuring your code behaves as expected.

  1. Return Statement
  2. Break Statement 
  3. Throw Statement

Return Statement

A function’s “return” statement returns a specified value. The execution of a function is suspended when a return statement is used in the function body.  The return statement was used to produce a specific value in response to a certain condition and end the function.

For Example 

Return” Statement used in the “ JavaScript exit function ”

Program 1

function returnFunc(a, b) {
    if (a>80)  {
    return('you are a brilliant student.');
else if (b<60){
    return('you are average student');
    return("You are below average student")


You are a brilliant student.

Explain the Return Statement program

We’ll first construct the function “returnFunc()” and send it the two parameters “a” and “b.” Then, for each condition, we will return a specified value:

The function will be called with the supplied arguments in the following step. As a result, the function will terminate, the specified value in either of the situations will be returned, and the relevant condition in the code above will be executed:


Here, the function ends after returning the equivalent value “You are a brilliant student.” against the “90” value in “a” in the output:

Also read: Dictionaries in C++

Break Statement

Break statement less traditional comparison to return statement. It is used for exit in the JavaScript function.

It terminates the current loop and jumps out of a code block. 

For Example

“Break” Statement used in the “ JavaScript exit function ”

infoFunc = () => {
    infoFunc: {
console.log("My name is Kalam.");
console.log("I am a student.");
console.log("My branch is CSE.");

break infoFunc;

console.log("I am the college topper.");
console.log("I am selected as a programmer in TCS Organization.");


info1Func = () => {
    info1Func: {
    console.log("My name is Rahul.");
    console.log("I am a BCA  student.");
    console.log("I am the college topper");
    console.log("I am selected as a programmer in TCS Organization.");



My name is Kalam.

I am a student.

My branch is CSE.

My name is Rahul.

I am a BCA  student.

I am the college topper.

I am selected as a programmer in TCS organization.


 “Breakstatement program.

In this method, we displayed five statements on the console and applied the “break” statements between them. As a result, the statement placed after the break statement will not be executed as the function because it will exit before it.

To do so, first, we will define two arrow functions named “infoFunc()” and “info1Func(),” which help to how we can use “break statement” in a function.  We will explain to you in detail we take two arrow functions. 

We will use break statements with the “infoFunc()”  arrow function. We will not use break statements with the “info1Func(),” arrow function. Because we will show what happens when we use the “break” statement in the arrow function or not using a break statement in the arrow function. 

Let’s start.

Now we will place a “break” statement between the operation of printing 5 statements for the first arrow function, “infoFunc().” 

We will not place a “break” statement between the operation of printing 4 statements for the second arrow function, “info1Func().”

Now, we will call both arrow functions “infoFunc()” and “info1Func(),” and the corresponding functionalities of the functions will be executed:

In the output, we can see that the fourth added statement after the break is not executed as the function exited before it, and we have not placed “break” between the second arrow function “info1Func()” statements, so it prints all statements.

Throw Statement

Throw Statements can exit a function, While try-catch is usually used for fetching data. 

The “try throw catch” commands handle conditions and exceptions.

“Try catch throw”  Statement used in the “ JavaScript exit function ”


escapeFunc = (work) => {
    try {
    if (work === "school") throw"exit";
    else {
    console.log("Today I am not going to school")
        } catch (e) {
    console.log("Today I am going to school")


Today I am going to school

Explaination “Try Catch Throw” statement program

First, we’ll create a function called “escapeFunc()” and send it the word “go” as input.

We’ll add a condition to the try block immediately. The function will terminate by displaying the supplied message if the condition is true and an exception is raised to the “catch” block with the keyword “e” standing for “exit”:

The function will then be called with the given inputs. An exception will therefore be raised, and the function will terminate:


We have discussed how we can exit from the javascript function in this “JavaScript Exit Function” article. We have discussed these three methods, which are helpful to you exit any function in javascript. 

You may use the “break” statement to end the loop and leave the function in JavaScript, the “try throw catch” commands to handle conditions and exceptions, and the “return” statement to return a value against a defined condition.

The return method is most popular in Javascript for the exit, but you have also used other methods per your comfort.

FAQ (Frequently Asked Questions)

How do I terminate a loop in JavaScript?

You can use the break statement to exit a loop early in JavaScript. The break statement can be used for loops, while loops, and do…while loops. When the break statement is executed, the loop terminates, and any code after the loop is executed.

How do I exit a recursive function in JavaScript?

In JavaScript, you can end a recursive function with the return statement. Any code that comes after the return statement is not performed when the return statement is invoked, which ends the function. The recursive function frequently has a condition that, if fulfilled, causes it to exit.

What is an exit function in JavaScript?

A function that ends the execution of a program or the current function is an exit function in JavaScript. Unlike many other programming languages, JavaScript is a single-threaded, non-blocking language, hence it doesn’t support the concept of terminating or stopping a program. A standard exit function is thus absent from JavaScript.