JavaScript SyntaxError – Illegal character

This JavaScript exception illegal character occurs if there is an invalid or unexpected token that doesn’t belong there in the code.

Message:

SyntaxError: Invalid character (Edge)
SyntaxError: illegal character (Firefox)
SyntaxError: Invalid or unexpected token (Chrome)

Error Type:

SyntaxError

Cause of Error: There is an invalid or unexpected token that doesn’t belong there in the code. Check the code for mismatches like a minus sign ( – ) and a dash ( – ) or simple quotes ( ” ) vs non-standard quotation marks ( “ ).

Example 1: In this example, there is not any mismatch, So the error has not occurred.



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>Syntax Error</title>
</head>
<body>
    <script>
      // Valid token
         var GFG_colors = ['#002', '#353', '#665']; 
      document.write(GFG_colors)
    </script>
</body>
</html>

chevron_right


Output:

#002,#353,#665

Example 2: In this example, there is a token mismatch, 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 Token
      var GFG_colors = ['#002', '#353', '#665']; 
      document.write(GFG_colors)
    </script>
</body>
</html>

chevron_right


Output(in console):

SyntaxError: Invalid or 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.