return

Summary

Specifies the value to be returned by a function.

Version Information

Statement
Implemented in: JavaScript 1.0, NES 2.0
ECMA Version: ECMA-262

Syntax

return [ expression ];

Parameters

expression
The expression to return. If omitted, undefined is returned instead.

Examples

Example: Using return

The following function returns the square of its argument, x, where x is a number.

function square(x) {
   return x * x;
}

Example: Interrupt a function

A function immediately stops at the point where return is called.

function counter() {
  for (var count=1; ; count++) {  // infinite loop
    document.write (count+"A-");  // until 5
      if (count == 5) {
        return;
      }
      document.write (count+"B<br>");  // until 4
    }
  document.write (count+"C");  // never appears
}
counter();

Output:

1A-1B
2A-2B
3A-3B
4A-4B
5A-

return;  return true;  return false;  or  return {variable};  all have the same effect of breaking the function.

See also

Functions

Document Tags and Contributors

Last updated by: Havvy,