JavaScript return Statement

Subject: JavaScript

The return statement in JavaScript is used inside a function to return a value to the place where the function was called. It also stops the function execution immediately.


Syntax

  • expression is optional.
  • If no value is specified, the function returns undefined by default.

Example 1: Returning a Value


Example 2: Early Exit from Function


Example 3: Returning Without a Value


Example 4: Returning an Object


Best Practices

  • Always place the return value on the same line as the return keyword.

✅ Correct:

❌ Incorrect:

This will return undefined due to JavaScript's automatic semicolon insertion.


Key Takeaways

  • return ends the function and optionally sends back a value.
  • If omitted, undefined is returned.
  • Use return to exit functions early based on conditions.
  • Be cautious with newline placement after return.
  • Functions can return any data type—numbers, strings, arrays, objects, and more.
Next : JS Loops