The JavaScript exception "return (or yield) not in function" occurs when a return
or yield
statement is called outside of a function.
The JavaScript exception "return (or yield) not in function" occurs when a return
or yield
statement is called outside of a function.
SyntaxError: Illegal return statement (V8-based) SyntaxError: return not in function (Firefox) SyntaxError: Return statements are only valid inside functions. (Safari)
function cheer(score) { if (score === 147) return "Maximum!"; } if (score > 100) { return "Century!"; } } // SyntaxError: return not in function
The curly brackets look correct at a first glance, but this code snippet is missing a {
after the first if
statement. Correct would be:
function cheer(score) { if (score === 147) { return "Maximum!"; } if (score > 100) { return "Century!"; } }
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Bad_return_or_yield