imulsion
imulsion

Reputation: 9040

Syntax error: Illegal return statement in JavaScript

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

<script type = 'text/javascript'>
var ask = confirm('".$message."');
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?

Upvotes: 92

Views: 209312

Answers (8)

Simon E.
Simon E.

Reputation: 58460

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.)

Upvotes: 2

Pietro
Pietro

Reputation: 11

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;
}

Upvotes: 1

Engineer
Engineer

Reputation: 8847

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

Upvotes: 0

user6269864
user6269864

Reputation:

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
}

Upvotes: 4

Oleksandr Popugaiev
Oleksandr Popugaiev

Reputation: 11

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>

Upvotes: 1

Sunny S.M
Sunny S.M

Reputation: 5978

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

Upvotes: 7

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

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.

Upvotes: 137

prabeen giri
prabeen giri

Reputation: 803

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!!

Upvotes: 6

Related Questions