What Causes “Uncaught SyntaxError: Illegal return statement” in JavaScript?

If you get Uncaught SyntaxError: Illegal return statement in your JavaScript console, it’s usually because you accidentally put a return statement (return) outside of a function.

This is not allowed:

// This throws an error because it’s outside of a function
return "David"

This is allowed:

// This works because return is inside a function
var myNameIs = function() {
  return "David"

Has this been helpful to you?

You can support my work by sharing this article with others, or perhaps buy me a cup of coffee 😊


Share & Discuss on