SyntaxError: return not in function
The JavaScript exception "return not in function" occurs when a return
statement is called outside of a function.
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?
Examples
Missing curly brackets
js
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:
js
function cheer(score) {
if (score === 147) {
return "Maximum!";
}
if (score > 100) {
return "Century!";
}
}