JavaScript SyntaxError – Unexpected token

This JavaScript exceptions unexpected token occur if a specific language construct was expected, but anything else is typed mistakenly. This could be a simple typing mistake.

Message:

SyntaxError: expected expression, got "x"
SyntaxError: expected property name, got "x" 
SyntaxError: expected target, got "x"
SyntaxError: expected rest argument name, got "x"
SyntaxError: expected closing parenthesis, got "x"
SyntaxError: expected '=>' after argument list, got "x"

Error Type:

SyntaxError

Cause of Error: There is a simple typing mistake in the code.

Example 1:



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>Unexpected Token Error</title>
</head>
<body>
    <script>
        for (let i = 0; i < 5; ++i) {
          document.write(i); 
        }
    </script>
</body>
</html>

chevron_right


Output:

01234

Example 2: There is a typing mistake in the code, So the error occurred.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>Unexpected Token Error</title>
</head>
<body>
    <script>
        for (let i = 0; i < 5,; ++i) {
          document.write(i); 
        }
    </script>
</body>
</html>

chevron_right


Output(in console):

SyntaxError: Unexpected token ';'

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.