“SyntaxError: Illegal return statement” in JavaScript is a syntax-related error. It describes that you are typing the command wrong or using the statement in the wrong place. Please read the following instructions to resolve it.
Why does JavaScript’s “SyntaxError: Illegal return statement” in JavaScript happen?
The most common reason for “SyntaxError: Illegal return statement” in Javascript is that you used the return statement incorrectly. As the error code describes, it’s a syntax-related error, indicating it’s a return statement. See the example below to better understand.
Here I have a very simple example: if the condition in the learn()
function is true, then a corresponding message is sent. But if I leave the return statement out of the function, it throws an error like this:
var slogan = "LearnShareIT"
function learn() {
if (slogan == "LearnShareIT") {
return "I am a developer!!";
}
}
//returns in the wrong place
return "I am a student!!";
console.log(learn());
Output
main.js:173 Uncaught SyntaxError: Illegal return statement (at main.js:173:1)
Or if I am missing a curly brace, it still gives me the same error:
var slogan = "LearnShareIT"
function learn() //missing here{
return slogan;
}
console.log(learn());
Output
Uncaught SyntaxError: Illegal return statement (at main.js:171:5)
Since a return statement follows the curly brace, it will still report an error.
To return an appropriate value to the function without causing an error, refer to the way below.
How to fix this error?
Use the correct command structure
Also the above example, I proceed to reset the position of the return command, and immediately it shows the result I want as follows:
var slogan = "LearnShareIT"
function learn() {
if (slogan == "LearnShareIT") {
return "I am a developer!!";
}
//put the return command in the right place
return "I am a student!!";
}
console.log(learn());
Output
I am a developer!!
For the missing function brackets, we need to add the correct syntax, and the code will run smoothly:
var slogan = "LearnShareIT"
function learn() {
return slogan;
}
console.log(learn());
Output
LearnShareIT
Shorten the number of return statements
Too many return statements in a function will cause trouble for your code. Return is a command directly related to the computer’s memory. If you abuse it, you will get an error.
Example
var fruit = 'apple';
function learn() {
if (fruit == 'apple') {
return 'apple';
}
else {
return 'banana';
}
//returns in the wrong place
return 'orange';
//returns in the wrong place
}return 'melon';
Output
main.js:186 Uncaught SyntaxError: Illegal return statement (at main.js:186:2)
Using too many return statements will mess up the code and you might end up putting it in the wrong place, so shorten it like this:
var fruit = 'apple';
function choose() {
if (fruit == 'apple') {
return 'apple';
}
else {
return 'banana';
}
}
console.log(choose());
Output
apple
Summary
For the error “SyntaxError: Illegal return statement” in JavaScript, you need to understand what you want your function to return so you can use return correctly. Using return indiscriminately poses a huge risk to your code, so be aware. Above are solutions to help you solve this error, good luck with your studies.
Maybe you are interested:

My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css