You can exit from any JS function using the return keyword.

It can be placed anywhere in your code.

const exampleFunction = () => {
  console.log('hello');
  
  return;
  
  console.log('world');
  console.log('!');
}

The example above is quite artificial, but it demonstrates how return works very well. When you call the exampleFunction only the string hello will be printed to the console.

You can also add a value after the return. In this case the value you’ve specified will become the “result” of the function execution of the “return value”.

const exampleFunction = () => {
  console.log('hello');
  
  return 1;

  console.log('world');
  console.log('!');
}

const capturedReturnValue = exampleFunction();

console.log(capturedReturnValue);  // 1

Here we’ve saved the return value of the function exampleFunction to the variable capturedReturnValue and then logged it to the console.

Output:

hello
1

If you don’t add a return keyword anywhere in your function, then it will exit when it executes its last statement.

const exampleFunction = () => {
  console.log('hello');
  console.log('world');
  console.log('!');
}

Output:

hello
world
!