JavaScript SyntaxError – Missing ; before statement

This JavaScript exception missing ; before statement occurs if there is a semicolon (;) missing in script.

Message:

SyntaxError: Expected ';' (Edge)
SyntaxError: missing ; before statement (Firefox)

Error Type:

SyntaxError

Cause of Error: Somewhere in code, there is a missing semicolon (;). You need to provide it so that JavaScript can parse the source code without any error.

Example 1: In this example, the string is not escaped properly and JavaScript expecting a “;”, so the error has occurred.



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>Syntax Error</title>
</head>
<body>
    <script>
        // invalid string
        var GFG = 'This is GFG's platform';  
        document.write(GFG);
    </script>
</body>
</html>

chevron_right


Output(In console of Edge Browser):

SyntaxError: Expected ';'

Example 2: In this example, the properties of an object is declared with the var declaration, Which is invalid. So the error has occurred, 

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>Syntax Error</title>
</head>
<body>
    <script>
        var GFG = {};
        var GFG.prop_1 = 'Val_1';  
        document.write(JSON.stringify(GFG));
    </script>
</body>
</html>

chevron_right


Output(In console of Edge Browser): 

SyntaxError: Expected ';'

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.