How to exit a function in JavaScript?

To stop a function in JavaScript, use the return keyword.

You can either exit the function without returning anything:

function logIfMoreThanTwo(value) {
  if (value <= 2) {
    return;
  }

  console.log(value);
}

Or return some value by putting it after the return statement:

function twoOrThree(value) {
  if (value) {
    return 2;
  }

  return 3;
}