This write-up will guide you to exit a JavaScript Function.
How to Exit a JavaScript Function?
To exit a JavaScript Function, you can use:
We will now go through each of the mentioned approaches one by one!
Method 1: Exit a JavaScript Function Using return Statement Method
The “return” statement returns a specific value of a function. We applied the return statement to output a particular value against a specified condition and exit the function.
Look at the below-stated example.
Example
Firstly, we will define a function “exitFunc()” and pass two arguments “a” and “b” in it. Then, we will return a specific value against each condition:
if (a>5) {
return(0);
}
else
{
return("true")
}
}
In the next step, we will call the function with the specified arguments. As a result, the corresponding condition in the above code will be executed, and the specific value in either of the cases will be returned, and the function will exit:
Here, in the output, the corresponding value “true” is returned against the “3” value in “a” and the function exits:
Method 2: Exit a JavaScript Function Using “try catch throw” Statement Method
The “try catch throw” statements work in such a way that if an error occurs in the try block, the throw statement throws the exception to the catch block which then handles that exception. In this method, a condition is defined in the “try” block and the function exits by throwing an exception “exit” to the catch block. The catch block then exits the function by displaying the message on the console.
Moreover, we have also used “else” condition to check if the function exited properly. In the other case, it will display the “Function not exited” message.
Example
First, we will place a condition in the try block. If the condition is true, an exception will be thrown to the “catch” block with “e” referring to “exit” and the function will exit by displaying the specified message:
try {
if (name === "JavaScript") throw"exit";
else {
console.log("Function not exited")
}
} catch (e) {
console.log("The Function is exited")
}
};
Lastly, the function will be called with the specified arguments. As a result, an exception will be thrown and the function will exit:
Output
Method 3: Exit a JavaScript Function Using break Statement Method
The “break” statement jumps out of a code block and terminates the current loop.
Here is an example for the demonstration.
Example
In this method, we displayed two statements on the console and applied the “break” statement in between them. As a result, the statement placed after the break statement will not be executed as the function because the function will exit before it.
To do so, first, we will define an arrow function named “exitFunc()” and place a “break” statement between the operation of printing two statements:
exitFunc: {
console.log("Function is exited");
break exitFunc;
console.log("Function not exited");
}
};
Next, we will call the “exitFunc()” function and the corresponding functionalities of the function will be executed:
In the output, we can see that the second added statement after the break is not executed as the function exited before it:
We have provided the simplest methods to exit a JavaScript Function.
Conclusion
To exit a JavaScript Function, you can use the “return” statement to return a value against a specified condition, the “try throw catch” statements to handle the conditions and exceptions and the “break” statement to terminate the loop and jump out from the function. This article guided the methods related to exiting a JavaScript function.