🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

return Statement

AI & DATA SCIENCE // return

return exits a function and optionally sends a value back to the caller. A function with no return (or bare return) returns undefined.

Syntax

function double(n) {
  return n * 2;
}
const result = double(5); // 10

Deep Dive Course

Every function in JavaScript implicitly returns **undefined** if no `return` statement is reached. A `return` statement immediately exits the function. Multiple `return` statements in different branches are valid and common for guard clauses.

1Understanding return Statement

Every function in JavaScript implicitly returns undefined if no return statement is reached. A return statement immediately exits the function. Multiple return statements in different branches are valid and common for guard clauses.

💡

Don't put a newline between return and the return value — ASI will insert a semicolon and your function will return undefined.

editor.html
// Guard clause pattern
function divide(a, b) {
  if (b === 0) return null; // guard
  return a / b;
}

console.log(divide(10, 2));  // 5
console.log(divide(10, 0));  // null
localhost:3000

2Practical Example

Here is a real-world application of return Statement showing how it is used in production JavaScript code.

editor.html
// ASI pitfall with return
function broken() {
  return    // ASI inserts semicolon here!
  { value: 42 }; // never reached
}

function fixed() {
  return {
    value: 42 // opening brace on same line
  };
}
console.log(broken()); // undefined
console.log(fixed());  // { value: 42 }
localhost:3000

3Best Practices

Follow these guidelines when working with return Statement:

1. Use early returns (guard clauses) to reduce nesting

2. Always explicitly return from functions that are expected to produce values

3. Avoid return on its own line with value on the next line — ASI bug

⚠️

Tip: Don't put a newline between return and the return value — ASI will insert a semicolon and your function will return undefined.

editor.html
// Guard clause pattern
function divide(a, b) {
  if (b === 0) return null; // guard
  return a / b;
}

console.log(divide(10, 2));  // 5
console.log(divide(10, 0));  // null
localhost:3000

Examples

Example 01Basic Usage
// Guard clause pattern
function divide(a, b) {
  if (b === 0) return null; // guard
  return a / b;
}

console.log(divide(10, 2));  // 5
console.log(divide(10, 0));  // null
Example 02Advanced Example
// ASI pitfall with return
function broken() {
  return    // ASI inserts semicolon here!
  { value: 42 }; // never reached
}

function fixed() {
  return {
    value: 42 // opening brace on same line
  };
}
console.log(broken()); // undefined
console.log(fixed());  // { value: 42 }

Best Practices

  • Use early returns (guard clauses) to reduce nesting
  • Always explicitly return from functions that are expected to produce values
  • Avoid return on its own line with value on the next line — ASI bug

Interview Question

What does a function return if it has no return statement?

Hint: All functions return something.

Every JavaScript function returns undefined by default if no return statement is present or if return is used without a value. Constructor functions called with 'new' are an exception — they return the newly created object unless a non-primitive is explicitly returned.

Exercises

MediumPractice using return Statement in a real scenario.
View Solution
// Guard clause pattern
function divide(a, b) {
  if (b === 0) return null; // guard
  return a / b;
}

console.log(divide(10, 2));  // 5
console.log(divide(10, 0));  // null

Frequently Asked Questions

What does a function return if it has no return statement?

Every JavaScript function returns undefined by default if no return statement is present or if return is used without a value. Constructor functions called with 'new' are an exception — they return the newly created object unless a non-primitive is explicitly returned.

Related Functions

Function-DeclarationArrow-FunctionsRecursive-Functions