Os nossos voluntários ainda não traduziram este artigo para Português (Europeu). Ajude-nos a concluir o trabalho!
Pode também ler o artigo em English (US).

The return statement ends function execution and specifies a value to be returned to the function caller.


return [[expression]]; 
The expression whose value is to be returned. If omitted, undefined is returned instead.


When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller. For example, the following function returns the square of its argument, x, where x is a number.

function square(x) {
   return x * x;
var demo = square(3);
// demo will equal 9

If the value is omitted, undefined is returned instead.

The following return statements all break the function execution:

return true;
return false;
return x;
return x + y / 3;

Automatic Semicolon Insertion

The return statement is affected by automatic semicolon insertion (ASI). No line terminator is allowed between the return keyword and the expression.

a + b;

is transformed by ASI into:

a + b;

The console will warn "unreachable code after return statement".

Starting with Gecko 40 (Firefox 40 / Thunderbird 40 / SeaMonkey 2.37), a warning is shown in the console if unreachable code is found after a return statement.


Interrupt a function

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

function counter() {
  for (var count = 1; ; count++) {  // infinite loop
    console.log(count + 'A'); // until 5
      if (count === 5) {          
      console.log(count + 'B');  // until 4
  console.log(count + 'C');  // never appears


// Output:
// 1A
// 1B
// 2A
// 2B
// 3A
// 3B
// 4A
// 4B
// 5A

Returning a function

See also the article about Closures.

function magic(x) {
  return function calc(x) { return x * 42; };

var answer = magic();
answer(1337); // 56154


Specification Status Comment
ECMAScript 1st Edition (ECMA-262) Standard Initial definition.
ECMAScript 5.1 (ECMA-262)
The definition of 'Return statement' in that specification.
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Return statement' in that specification.
ECMAScript Latest Draft (ECMA-262)
The definition of 'Return statement' in that specification.
Living Standard  

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic Support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)
FeatureAndroidChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
Basic Support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

See also

Etiquetas do documento e contribuidores

 Última atualização por: KrisTransue,