SyntaxError: return not in function
The JavaScript exception "return (or yield) not in function" occurs when a
return
or yield
statement is called outside of a function.
Message
SyntaxError: 'return' statement outside of function (Edge)
SyntaxError: return not in function (Firefox)
SyntaxError: yield not in function (Firefox)
Error type
What went wrong?
Examples
Missing curly brackets
var cheer = function(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:
var cheer = function(score) {
if (score === 147) {
return 'Maximum!';
}
if (score > 100) {
return 'Century!';
}
};