How To Fix JavaScript Uncaught SyntaxError: Invalid Or Unexpected Token

I Include an external javascript file in an Html file, when I browse the Html file in a web browser, I meet the javascript error with the error message Uncaught SyntaxError: Invalid or unexpected token. This article will tell you how to fix it.

1. How To Reproduce The JavaScript Uncaught SyntaxError: Invalid Or Unexpected Token.

  1. The Html file name is test.html.
  2. In the test.html file, it imports an external javascript file test.js using the below code.
    <script type="text/javascript" src="test.js" ></script>
  3. Open the Html file test.html in the Google Chrome web browser.
  4. Right-click the web page in the web browser, then click the Inspect menu item in the popup menu list to open the web browser Inspector window.
  5. Click the Console tab on the web browser’s Inspector window.
  6. Then you can see the error message Uncaught SyntaxError: Invalid or unexpected token in the console output.

2. How To Fix The JavaScript Uncaught SyntaxError: Invalid Or Unexpected Token.

  1. The main reason for such error is because there are some unnormal characters in the javascript code that are not encoded in the correct charset.
  2. You just need to remove the javascript code in the error line, and to see whether the error disappears.
  3. Do not forget to refresh the test.html and the test.js file to make the change take effect.
  4. You can also copy the javascript code line that has the error occurs to the text editor such as Sublime Text.
  5. Then you can find the unnormal character in it and remove it to fix the error.
    <0x200b>​// Create a SnowFlakeApp instance when the window is loaded. 
    window.onload = new SnowFlakeApp();
  6. In the above example javascript code, the character <0x200b> is unnormal, remove it then you can fix the error.
5 1 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Clicky
0
Would love your thoughts, please comment.x
()
x