Syntax error: Illegal return statement in JavaScript

后端 未结 6 1495
南方客
南方客 2021-02-04 23:25

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



        
相关标签:
6条回答
  • 2021-02-04 23:34

    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!!
    
    0 讨论(0)
  • 2021-02-04 23:40

    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

    0 讨论(0)
  • 2021-02-04 23:47

    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
    }
    
    0 讨论(0)
  • 2021-02-04 23:47

    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
    
    0 讨论(0)
  • 2021-02-04 23:50

    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>
    
    0 讨论(0)
  • 2021-02-04 23:54

    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.

    0 讨论(0)
提交回复
热议问题