The return statement ends a function and sends a value back to the code that called it. If a function has no return, or uses return without a value, the call evaluates to undefined. This guide explains how javascript return works, what happens to code after it, and the traps that catch beginners.
A function that returns a value can be used inside expressions, stored in variables and passed to other functions. A function that only does something, such as logging, usually returns nothing.
Write return followed by an expression. The expression is evaluated, the function stops, and the result becomes the value of the call.
The call square(6) is replaced by 36 wherever it appears, so you can use it inside larger expressions.
return exits the function immediately. Any statement after it in the same path is unreachable.
This is useful for early returns. Handling the invalid or simple cases first keeps the main logic flat instead of nested in many else blocks.
A function with no return produces undefined. A bare return; does the same.
A common bug is forgetting return inside a function meant to compute something, then wondering why the result is undefined. Array callbacks have the same trap: map with braces needs an explicit return.
Fix it by writing return n * 2; or by dropping the braces.
A function returns exactly one value. To give back several, wrap them in an array or an object.
Destructuring pulls the parts out neatly. Prefer an object when the parts have names, and an array for a short, ordered pair.
JavaScript inserts a semicolon after return if the line ends there. A value on the next line is ignored.
Always start the returned expression on the same line as return. Wrap a long expression in parentheses if you want to break it over several lines.
Arrow functions can return implicitly when the body is a single expression without braces. To return an object literal that way, wrap it in parentheses.
Without the parentheses, the braces would be read as a function body and the function would return undefined.
A function can return another function, which is the basis of closures and factories. An async function always returns a promise, and the value you return becomes the promise's resolved value.
Inside an async function, return never gives the caller a plain value. The caller must use await or .then() to read it.
undefined. That is the result of any call to a function that has no return statement or uses return; with no value.
Not directly. Return an array or an object containing the values, then destructure the result.
No. It only ends the current function call. Execution continues in the caller.
console.log prints a value for you to read, while return hands the value back to the code that called the function.
return ends a function and delivers a value to the caller. Without it, the result is undefined. Use early returns to simplify logic, return objects or arrays for several values, keep the value on the same line as return, and wrap object literals in parentheses in arrow functions.