Friday, September 5, 2025
HomeLanguagesJavascriptJavaScript SyntaxError – Unexpected token

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




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


Output:

01234

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

HTML




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


Output(in console):

SyntaxError: Unexpected token ';'
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11866 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS