HTML: Markup language
CSS: Styling language
JavaScript: Scripting language
Web APIs: Programming interfaces
All web technology
Learn web development
Discover our tools
Get to know MDN better
このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。
View in English Always switch to English
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
return 文は関数の実行を終了し、関数の呼び出し元に返す値を指定します。
return
function getRectArea(width, height) { if (width > 0 && height > 0) { return width * height; } return 0; } console.log(getRectArea(3, 4)); // 予想される結果: 12 console.log(getRectArea(-3, 4)); // 予想される結果: 0
return; return expression;
expression
値が返される式。省略した場合は、代わりに undefined が返されます。
undefined
return 文は関数本体内でのみ使用できます。関数本体内で return 文が使用されると、関数の実行は停止します。return 文は配置される関数によって効果が異なります。
next()
{ done: true, value: returnedValue }
return 文が try ブロック内で実行された場合、finally ブロックがあれば、実際に値が返される前に実行されます。
try
finally
構文上、return キーワードと返値の式の間の改行は許容されません。
return a + b;
上記のコードは自動セミコロン挿入 (ASI) によって、次のように変換されます。
return; a + b;
これにより、この関数は undefined を返し、 a + b の式は評価されません。これはコンソールに警告を表示することがあります。
a + b
括弧を使用することで、この問題を回避する(ASI を防ぐ)ことができます。
return ( a + b );
関数は return が呼び出された時点で即座に終了します。
function counter() { // 無限ループ for (let count = 1; ; count++) { console.log(`${count}A`); // 5 まで if (count === 5) { return; } console.log(`${count}B`); // 4 まで } console.log(`${count}C`); // 決して現れない } counter(); // ログ: // 1A // 1B // 2A // 2B // 3A // 3B // 4A // 4B // 5A
クロージャについての記事も参照のこと。
function magic() { return function calc(x) { return x * 42; }; } const answer = magic(); answer(1337); // 56154
Enable JavaScript to view this browser compatibility table.