Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The return statement terminates the execution of a function and specifies a value to be passed back to the function’s caller. When the JavaScript engine encounters this statement, control is transferred out of the current function’s execution context and back to the calling execution context, provided the return is not intercepted by a finally block.
return [expression];

Scope Constraints

The return statement is lexically constrained to the body of a function. Attempting to use a return statement outside of a function body—such as at the top level of a standard script or an ES module—will result in a SyntaxError.

Mechanics and Behavior

Expression Evaluation The expression following the return keyword is optional. If provided, the engine evaluates the expression and returns the resulting value.
function evaluateExpression() {
  return 10 * 5; // Evaluates to 50, then returns 50
}
Execution Termination and finally Blocks A return statement generally acts as an immediate exit point. Statements within the function block that lexically follow a reachable return statement are typically classified as dead code and will not execute. However, if a return statement is executed inside a try or catch block, the JavaScript engine will intercept the termination to execute the code within a corresponding finally block before actually leaving the function’s execution context.
function haltExecution() {
  return true;
  console.log("This is dead code and will not execute."); 
}

function finallyIntercept() {
  try {
    return "try block return";
  } finally {
    console.log("This executes before the function terminates.");
  }
}
Implicit Returns If a function’s execution reaches the end of its block without encountering a return statement, or if the return keyword is used without an accompanying expression, the JavaScript engine implicitly returns the primitive value undefined.
function implicitUndefined() {}
function explicitUndefined() { return; }

console.log(implicitUndefined()); // undefined
console.log(explicitUndefined()); // undefined

Automatic Semicolon Insertion (ASI) Caveat

JavaScript’s lexical grammar enforces a strict rule regarding line terminators and the return statement. A line break cannot be placed between the return keyword and its corresponding expression. If a line terminator separates the two, the engine’s Automatic Semicolon Insertion (ASI) mechanism implicitly inserts a semicolon immediately after the return keyword. This causes the function to return undefined, and the intended expression is treated as an unreachable standalone statement. Incorrect Syntax (ASI Trap):
function getObject() {
  return 
  {
    status: "active"
  };
}
// Engine interprets as:
// return; 
// { status: "active" }; 
// Returns: undefined
Correct Syntax: To return an object literal or a multi-line expression, the expression must begin on the same line as the return keyword, typically utilizing grouping operator parentheses.
function getObject() {
  return {
    status: "active"
  };
}

// OR using grouping parentheses for multi-line expressions
function getObjectSafe() {
  return (
    {
      status: "active"
    }
  );
}
Master JavaScript with Deep Grasping Methodology!Learn More