What does the return statement do? When do I use return statements?


#1

Question

What does the return statement do? When do I use return statements?

Answer

return statements can be used inside functions to: 1. end function execution, which will stop any code under the return statement from running and 2. return a value when the function is called. return statements cannot be used outside of functions. The default return value is undefined when no return value is specified.
For example:

const myFunc = () => {
  console.log('hello'); //function that logs hello to the console, no return value is specified
}
console.log(myFunc()); //logging the function call executes the function and logs the return value as well, in this case no return value was specified so undefined also logs to the console