Syntax error: Illegal return statement in JavaScript

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

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



        
6条回答
  •  無奈伤痛
    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
    }
    

提交回复
热议问题