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.
Table of Contents
- Browser Support
- Other Related Concepts
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.
- 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.