Get to know MDN better
The JavaScript exception "return not in function" occurs when a return statement is called outside of a function.
A return statement is called outside of a function. Maybe there are missing curly braces somewhere? The return statement must be in a function, because it ends function execution and specifies a value to be returned to the function caller.
The curly braces look correct at a first glance, but this code snippet is missing a { after the first if statement. Correct would be:
This page was last modified on Jul 8, 2025 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.