return Statement

Exits from the current function and returns a value from that function.

return[(][expression][)]

Arguments

  • expression
    Optional. The value to be returned from the function. If omitted, the function does not return a value.

Remarks

You use the return statement to stop execution of a function and return the value of expression. If expression is omitted, or no return statement is executed from within the function, the expression that called the current function is assigned the value undefined.

Execution of the function stops when the return statement is executed, even if there are other statements still remaining in the function body. The exception to this rule is if the return statement occurs within a try block, and there is a corresponding finally block, the code in the finally block will execute before the function returns.

If a function returns because it reaches the end of the function body without executing a return statement, the value returned is the undefined value (this means the function result cannot be used as part of a larger expression).

Note

The code in a finally block is run after a return statement in a try or catch block is encountered, but before that return statement is executed. In this situation, a return statement in the finally block is executed before the initial return statement, allowing for a different return value. To avoid this potentially confusing situation, do not use a return statement in a finally block.

Example

The following example illustrates the use of the return statement.

function myfunction(arg1, arg2){
   var r;
   r = arg1 * arg2;
   return(r);
}

Requirements

Version 1

See Also

Reference

function Statement

try...catch...finally Statement