Categories
JavaScript Answers

How to Fix the ‘SyntaxError: return not in function’ Error in Our JavaScript App?

Spread the love

Sometimes, we may run into the ‘SyntaxError: return not in function’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘SyntaxError: return not in function’ when we’re developing JavaScript apps.

Fix the ‘SyntaxError: return not in function’ When Developing JavaScript Apps

To fix the ‘SyntaxError: return not in function’ when we’re developing JavaScript apps, we should make sure we’re using return and yield property inside a function.

On Edge, the error message for this error is SyntaxError: 'return' statement outside of function.

On Firefox, the error message for this error is SyntaxError: return not in function and SyntaxError: yield not in function .

For instance, if we have:

const cheer = function(score) {
  if (score === 147)
    return 'Maximum!';
  };
  if (score > 100) {
    return 'Century!';
  }
}

then we’ll get the error since the first if statement is missing the opening curly brace.

To fix it, we write:

const cheer = function (score) {
  if (score === 147) {
    return "Maximum!";
  }
  if (score > 100) {
    return "Century!";
  }
};

Conclusion

To fix the ‘SyntaxError: return not in function’ when we’re developing JavaScript apps, we should make sure we’re using return and yield property inside a function.

On Edge, the error message for this error is SyntaxError: 'return' statement outside of function.

On Firefox, the error message for this error is SyntaxError: return not in function and SyntaxError: yield not in function .

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *