Syntax error: Illegal return statement in JavaScript

Syntax error: Illegal return statement in JavaScript

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 dont break just because of an apostrophe in the message.

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

Syntax error: Illegal return statement in JavaScript

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

Leave a Reply

Your email address will not be published.