DEV Community

Cover image for Exit Function (Javascript)
Edgaras Stepanovas
Edgaras Stepanovas

Posted on • Edited on

Exit Function (Javascript)

As developers (a.k.a pro googlers), we all know how frustrating it can be to write code that doesn’t work as intended. We might spend hours trying to debug our code, only to find that a small typo or a misplaced semicolon was the culprit. That’s where the exit function comes in handy.

What is the Exit Function in JavaScript?

Think of the exit function as your emergency exit from a function. It’s like when you’re at a party and you suddenly realize you have to leave because you forgot to turn off the stove (yes, it happens!). You don’t want to waste any more time at the party, you just want to leave immediately. That’s what the exit function does — it lets you exit a function immediately and return a value to the caller. Cool, right?

Let’s take a look at an example. Imagine you’re writing a function to calculate the area of a rectangle. You might write something like this:

function calculateArea(length, width) {
  let area = length * width;
  return area;
}
Enter fullscreen mode Exit fullscreen mode

But what happens if the user enters a negative value for either length or width? Your function will still calculate the area, but it won’t be a valid value. That’s where the exit function comes in.

You can modify the function to check for negative values and exit early if they’re found:

function calculateArea(length, width) {
  if (length < 0 || width < 0) {
    return "Error: Length and width must be positive values.";
  }
  let area = length * width;
  return area;
}
Enter fullscreen mode Exit fullscreen mode

Now, if the user enters a negative value for either length or width, the function will exit immediately and return an error message. No more wasting time calculating the area of an invalid rectangle! Yay!

Here’s a more complex example that demonstrates how you can use the exit function to return a function from another function (huh?):

function createCalculator(operator) {
  if (operator === "+") {
    return function add(num1, num2) {
      return num1 + num2;
    }
  } else if (operator === "-") {
    return function subtract(num1, num2) {
      return num1 - num2;
    }
  } else {
    return function() {
      return "Error: Invalid operator.";
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

In this example, we’re defining a function called createCalculator that takes an operator as a parameter. If the operator is "+" or "-", the function returns a function that adds or subtracts two numbers, respectively. If the operator is neither "+" nor "-", the function returns an error message.

TESTING TIME! Let’s test this function with some examples:

const addNumbers = createCalculator("+");
console.log(addNumbers(2, 3)); // Output: 5

const subtractNumbers = createCalculator("-");
console.log(subtractNumbers(5, 3)); // Output: 2

const divideNumbers = createCalculator("/");
console.log(divideNumbers(10, 2)); // Output: Error: Invalid operator.
Enter fullscreen mode Exit fullscreen mode

As you can see, when we call createCalculator with the "+" operator, it returns a function called add that adds two numbers. Similarly, when we call createCalculator with the "-" operator, it returns a function called subtract that subtracts two numbers.

However, when we call createCalculator with the "/" operator, it doesn't return a function that can divide two numbers. Instead, it returns a function that simply returns an error message.

In conclusion, the exit function in JavaScript can be a lifesaver when you need to exit a function early and return a value to the caller. By using the exit function creatively, you can write more efficient and flexible code that can handle a variety of use cases.

If you enjoyed this article and want to learn more about JavaScript or programming in general, be sure to follow me for more helpful tips and tricks. Happy coding!

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

You'd probably be better off actually raising an error than returning a string where maybe the calling code was expecting a number. The calling code then has the option to decide what to do with the error - using the built in features of JS for error handling rather than function specific response checking to work out if there has been an error.

Collapse
 
edgaras98 profile image
Edgaras Stepanovas

Good point! Example was just for reference, the code could be optimized :) Thank you.