Computer >> Computer tutorials >  >> Programming >> Javascript

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"
}