Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

return

BaselineWidely available

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

Try it

function getRectArea(width, height) {  if (width > 0 && height > 0) {    return width * height;  }  return 0;}console.log(getRectArea(3, 4));// Expected output: 12console.log(getRectArea(-3, 4));// Expected output: 0

Syntax

js
return;return expression;
expressionOptional

The expression whose value is to be returned. If omitted,undefined is returned.

Description

Thereturn statement can only be used within function bodies. When areturn statement is used in a function body, the execution of the function is stopped. Thereturn statement has different effects when placed in different functions:

  • In a plain function, the call to that function evaluates to the return value.
  • In an async function, the produced promise is resolved with the returned value.
  • In a generator function, the produced generator object'snext() method returns{ done: true, value: returnedValue }.
  • In an async generator function, the produced async generator object'snext() method returns a promise fulfilled with{ done: true, value: returnedValue }.

If areturn statement is executed within atry block, itsfinally block, if present, is first executed, before the value is actually returned.

Automatic semicolon insertion

The syntax forbids line terminators between thereturn keyword and the expression to be returned.

js
returna + b;

The code above is transformed byautomatic semicolon insertion (ASI) into:

js
return;a + b;

This makes the function returnundefined and thea + b expression is never evaluated. This may generatea warning in the console.

To avoid this problem (to prevent ASI), you could use parentheses:

js
return (  a + b);

Examples

Interrupt a function

A function immediately stops at the point wherereturn is called.

js
function counter() {  // Infinite loop  for (let count = 1; ; count++) {    console.log(`${count}A`); // Until 5    if (count === 5) {      return;    }    console.log(`${count}B`); // Until 4  }  console.log(`${count}C`); // Never appears}counter();// Logs:// 1A// 1B// 2A// 2B// 3A// 3B// 4A// 4B// 5A

Returning a function

See also the article aboutClosures.

js
function magic() {  return function calc(x) {    return x * 42;  };}const answer = magic();answer(1337); // 56154

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-return-statement

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp