SyntaxError: Illegal return statement in JavaScript [Fixed]

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
3 min

banner

# SyntaxError: Illegal return statement in JavaScript

The "SyntaxError: Illegal return statement" occurs when the return statement is used outside of a function.

To solve the error make sure to only use the return statement inside of a named or an arrow function. The statement ends a function's execution and returns the value to the caller.

illegal return statement

Here are examples of when the error occurs.

index.js
// ⛔️ don't return outside a function return 42; // ✅ Use return inside a function function getNum() { return 42; } if (true) { // ⛔️ don't return outside a function return 10; } for (let i = 0; i < 3; i++) { // ⛔️ don't return outside a function return 15; } function example() // 👈️ has a missing curly brace { return true }

# Using the return statement outside a function

The most common cause of the error is using the return statement outside of a function.

Make sure to only use the return keyword inside functions.

index.js
// ✅ return inside named function function getNum() { return 42; } // ✅ return inside arrow function const example = () => { return 42; }; // ✅ implicit return using arrow function const another = () => 'bobbyhadz.com'; // ✅ implicitly return object from arrow function const another2 = () => ({ id: 1, site: 'bobbyhadz.com', });

use return statement only inside function

The code for this article is available on GitHub

The following if statement and for loop are not inside of a function, so we aren't allowed to use the return statement in them.

index.js
if (true) { // ⛔️ don't return outside a function return 10; } for (let i = 0; i < 3; i++) { // ⛔️ don't return outside a function return 15; }

Instead, you should wrap the if statement in a function.

index.js
function test() { if (true) { return 100; } return 200; }

# Use the break statement if you need to exit a loop

If you need to exit a for loop, you should use a break statement instead of return.

index.js
const arr = ['bobby', 'hadz', 'com']; for (let index = 0; index < arr.length; index++) { console.log(arr[index]); if (arr[index] === 'hadz') { // 👇️ exit the loop break; } }

use break statement to exit loop

The code for this article is available on GitHub

The break statement is used to exit a loop prematurely.

We can use the break statement in the following scenarios:

  • basic for loops
  • for ... of loops
  • for ... in loops
  • while loops
  • switch statements

Here is an example of using the break statement to exit a for...of loop.

index.js
let arr = ['a', 'b', 'c']; for (const element of arr) { if (element === 'b') { break; } console.log(element) // 👉️ a }

The if statement in the for...of loop checks if a condition is met on each iteration.

Once the if block runs, we use the break statement to exit the for...of loop.

Another common reason for the error being thrown is a syntax error in our code.

You can check on which line of your code the error was thrown by opening your browser's console or your Node.js terminal.

illegal return statement

In the screenshot, we can see that the error was thrown in the index.js file on line 5.

You can paste your code into an online Syntax Validator . The validator should be able to tell you on which line the error occurred.

You can hover over the squiggly red line to get additional information on why the error was thrown.

# Conclusion

To solve the "Illegal return statement" error:

  1. Make sure to only use the return statement inside of functions.
  2. Correct any syntax errors in your code.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.