Get to know MDN better
此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
return 陳述式會終止函式的執行,並指定要回傳給函式呼叫者的值。
要回傳其值的表達式。若省略,則回傳 undefined。
return 陳述式只能在函式主體內使用。當 return 陳述式用在函式主體中時,函式的執行將會停止。return 陳述式放置在不同的函式中會有不同的效果:
如果 return 陳述式在 try 區塊內執行,且該區塊有 finally 區塊,則 finally 區塊會先執行,然後才實際回傳該值。
語法禁止在 return 關鍵字與要回傳的表達式之間出現行終止符。
自動分號插入(ASI)會將上面的程式碼轉換為:
這會使函式回傳 undefined,且 a + b 表達式永遠不會被求值。這可能會在主控台中產生警告。
為了避免這個問題(防止 ASI),你可以使用括號:
函式會在呼叫 return 的當下立即停止。
參見關於閉包的文章。
| ECMAScript® 2027 Language Specification # sec-return-statement |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2026年1月7日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.