95

I am getting a really weird JavaScript error when I run this code:

<script type = 'text/javascript'>
var ask = confirm(<?=json_encode($message, JSON_HEX_TAG); ?>);
if (ask == false)
{
    return false;     
}

else
{
    return true;
}
</script>

In the JavaScript console it says:

Syntax Error: Illegal return statement

It occurs at return true; and return false;

(I am echoing this JavaScript from a PHP function; the $message variable is one of the PHP parameters)

What is wrong with my code?

4
  • 6
    Return-statements are only allowed inside of functions. What are you hoping to achieve with the return-statements in your example? Commented Apr 17, 2013 at 19:17
  • 27
    For people Googling this issue, you can also get this error if you are missing a { or }. Commented Sep 30, 2015 at 21:10
  • > The return statement ends function execution and specifies a value to > be returned to the function caller. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Apr 8, 2018 at 1:59
  • @Gavin or equivalently have an extra stray { or } Commented Jun 26, 2022 at 12:10

8 Answers 8

141

return only makes sense inside a function. There is no function in your code.

Also, your code is worthy if the Department of Redundancy Department. Assuming you move it to a proper function, this would be better:

return confirm(".json_encode($message).");

EDIT much much later: Changed code to use json_encode to ensure the message contents don't break just because of an apostrophe in the message.

Sign up to request clarification or add additional context in comments.

2 Comments

ah...oops...since i was writing this inside a php function i was trying to return values. thanks for your help :)
that would not be better in the context of my code...i cant even be bothered to explain why; it's so incredibly complicated
7

If you want to return some value then wrap your statement in function

function my_function(){ 

 return my_thing; 
}

Problem is with the statement on the 1st line if you are trying to use PHP

var ask = confirm ('".$message."'); 

IF you are trying to use PHP you should use

 var ask = confirm (<?php echo "'".$message."'" ?>); //now message with be the javascript string!!

Comments

7

in javascript return statement only used inside function block. if you try to use return statement inside independent if else block it trigger syntax error : Illegal return statement in JavaScript

Here is my example code to avoid such error :

<script type = 'text/javascript'>
(function(){
    var ss= 'no';
    if(getStatus(ss)){
        alert('Status return true');   
    }else{
        alert('Status return false'); 
    }

    function getStatus(ask){
        if(ask=='yes')
        {
        return true;     
        }
        else
        {
        return false;
        }
    }
})();
</script>

Please check Jsfiddle example

1 Comment

It makes no sense to return something from an IIFE -- the return value isn't used for anything.
4

In my experience, most often this error message means that you have put an accidental closing brace somewhere, leaving the rest of your statements outside the function.

Example:

function a() {
    if (global_block) //syntax error is actually here - missing opening brace
       return;
    } //this unintentionally ends the function

    if (global_somethingelse) {
       //Chrome will show the error occurring here, 
       //but actually the error is in the previous statement
       return; 
    }

    //do something
}

1 Comment

There's no function in the question.
3

where are you trying to return the value? to console in dev tools is better for debugging

<script type = 'text/javascript'>
var ask = confirm('".$message."');
function answer(){
  if(ask==false){
    return false;     
  } else {
    return true;
  }
}
console.log("ask : ", ask);
console.log("answer : ", answer());
</script>

Comments

3

For Node.js users:

I discovered that it's possible to use a return statement to exit the current JS script, but only for older-style CommonJS scripts that use the require(x) syntax.

Once you switch to the newer import x from y module syntax, using the return statement produces SyntaxError: Illegal return statement. Node will be expecting the newer syntax if your file extension ends in .mjs or if your package.json file contains "type": "module".

There's three potential workarounds if you want to be able to exit a module script early under certain conditions:

  1. Wrap your script in a function (perhaps a self-executing function) and use the return statement.

  2. Throw an error – although this will likely display an ugly stack trace, unless you also catch it

  3. (There's also process.exit(), but it's not recommended.)

Comments

1

just i forgot to declare the word 'function' before the function. es

myFunc(num)
{
   if(num > 0)
      return;
}

this produce 'illegal return statement' error, becouse miss 'function' before myFunc(num)

correct form:

function myFunc(num)
{
    if(num > 0)
       return;
}

1 Comment

There's no myFunc(num) in the question. What is this referring to?
0

This can happen in ES6 if you use the incorrect (older) syntax for static methods:

export default class MyClass
{
    constructor()
    {
       ...
    }

    myMethod()
    {
       ...
    }
}

MyClass.someEnum = {Red: 0, Green: 1, Blue: 2}; //works

MyClass.anotherMethod() //or
MyClass.anotherMethod = function()
{
   return something; //doesn't work
}

Whereas the correct syntax is:

export default class MyClass
{
    constructor()
    {
       ...
    }

    myMethod()
    {
       ...
    }

    static anotherMethod()
    {
       return something; //works
    }
}

MyClass.someEnum = {Red: 0, Green: 1, Blue: 2}; //works

2 Comments

There's no class code in the question.
That's true. Even so, those receiving the confusing error "Illegal return statement" who, via a search, arrive at this same question, as did I, might like answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.