Course contentsShow
← JavaScript
Lesson 419 of 2,72311. FunctionsFree lesson

Functions Without Explicit Returns

Understand that functions without a return statement implicitly return undefined.

Functions Without Explicit Returns

You'll learn: What happens when a JavaScript function doesn't explicitly return anything.

The Silent Return

Every JavaScript function always returns something, even when you don't write a return statement. When no explicit return exists, the function automatically returns undefined.

Think of it like asking someone a question. If they don't give you an answer, you're left with... nothing specific. That "nothing" in JavaScript is undefined.

How It Works

function greet(name) {
  console.log(`Hello, ${name}!`);
  // No return statement
}

const result = greet("Alice");
console.log(result); // undefined

The greet function does something (prints to console), but doesn't return a value. When we try to capture what it returns, we get undefined.

Common Scenarios

Functions that perform actions (side effects) often don't return values:

function updateDatabase(data) {
  // Imagine database operations here
  console.log("Database updated");
}

const outcome = updateDatabase({id: 1});
console.log(outcome); // undefined

Even functions with early returns can have code paths that implicitly return undefined:

function checkAge(age) {
  if (age >= 18) {
    return "Adult";
  }
  // If age < 18, no explicit return
}

console.log(checkAge(20)); // "Adult"
console.log(checkAge(15)); // undefined

Key Takeaway: Functions without an explicit return statement automatically return undefined — this is JavaScript's default behavior, not an error.