Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

SyntaxError: return not in function

The JavaScript exception "return not in function" occurs when areturn statement is called outside of afunction.

Message

SyntaxError: Illegal return statement (V8-based)SyntaxError: return not in function (Firefox)SyntaxError: Return statements are only valid inside functions. (Safari)

Error type

What went wrong?

Areturn statement is called outside of afunction. Maybe there are missing curly braces somewhere? Thereturn statement must be in a function, because it ends function execution and specifies a value to be returned to the function caller.

Examples

Missing curly braces

js
function cheer(score) {  if (score === 147)    return "Maximum!";  }  if (score > 100) {    return "Century!";  }}// SyntaxError: return not in function

The curly braces look correct at a first glance, but this code snippet is missing a{ after the firstif statement. Correct would be:

js
function cheer(score) {  if (score === 147) {    return "Maximum!";  }  if (score > 100) {    return "Century!";  }}

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp