How to Fix "Illegal return statement" Errors in JavaScript

Ferenc Almasi β€’ 2022 July 12 β€’ Read time 3 min read
  • twitter
  • facebook
JavaScript

The "Uncaught SyntaxError: Illegal return statement" error occurs, whenever JavaScript comes across a return statement outside of a function. The return keyword can only be used inside of functions. To resolve the error, make sure you don't use a return statement outside of functions.

Copied to clipboard! Playground
// ❌ Don't use return outside of functions
return 'Hello World! πŸ‘‹'

// βœ”οΈ Do
const greet = () => {
    return 'Hello World! πŸ‘‹'
}

// βœ”οΈ Or using a shorthand
const greet = () => 'Hello World! πŸ‘‹'

Using the return keyword inside block scope such as inside if statements or for loops are also not valid (if they are outside of a function).

Copied to clipboard! Playground
// ❌ Don't
if (user) {
    return `Hello ${user}! πŸ‘‹`
}

// ❌ Don't
for (let i = 100; i > 0; i--) {
    return i
}

In case you need to return a value from a loop conditionally, you need to introduce a variable outside of the loop, and then assign the value to the declared variable. And in case you need to break from a loop conditionally, then you can use the break keyword. 

Copied to clipboard! Playground
// Returning a value conditionally from a loop
let value

for (let i = 0; i < 100; i++) {
    if (conditionToBeMet) {
        value = i
    }
}
// now have the value of i
console.log(value)

// Breaking conditionally from a loop
for (let i = 100; i > 0; i--) {
    if (i < 50) {
        break
    }

    console.log(i)
}

In case you have nested loops and you want to only break from one of them, you can use a labeled statement, where you label your loop with a name, and pass the name after the break keyword. This way, you can control which loop should be stopped.

Copied to clipboard! Playground
outerLoop: {
    for (let i = 0; i < 100; i++) {
        innerLoop: {
            for (let j = 0; j < 100; j++) {
	        break innerLoop
	    }
        }
    }
}
Breaking from nested loops using a labeled statement

You may also come across a similar error message "Uncaught SyntaxError: Unexpected token 'return'". This error occurs whenever you have a syntax error in your application right above a return statement. For example:

Copied to clipboard!
const greet = user => {
    const message = 'Welcome ' + user +
    return message
}

Here we have an unexpected plus sign after the string, which means JavaScript will not expect the return keyword from the next line. In short:

Whenever you get an "Unexpected token 'return'" error in JavaScript, it means there's a syntax error (unclosed string, extra operator, missing keyword) right before your return statement.

  • twitter
  • facebook
JavaScript
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.