JavaScript 'Unexpected Token ILLEGAL' Error

Some errors in JavaScript are hard to find out and this is one of them, 'Unexpected Token ILLEGAL' message on your console can make you go crazy and re-check the code but if you didn't solve the problem read the post.

What can be worse than getting an error after writing a long JavaScript code ? 'Unexpected Token ILLEGAL' error is a frustrating one, it's often caused by an invalid character in the script. Most of the times you will find the unwanted character after thoroughly checking the script and then error is fixed but many times finding that unwanted character becomes a headache and to fix it we have to do something else than just checking the script again and again.

If you didn't find any invalid character in your code then it's obvious that the character is an hidden character and your are a JsFiddle fan. The invalid hidden character can be u200b (zero width space) or other hidden characters but how to find or fix it ? Good question, as the characters aren't visible how could we remove it ?

Removing whitespace in the code

If you want to fix it fast then the best way is to remove all the whitespace around and in the code and then run it. 

Testing your script with JSBin

If you use JsFiddle or some other code editing site/program which don't have support for ignoring or displaying invalid characters then this might always happen.

To check the code for an invalid characters use JsBin to find the invalid characters and remove it. Go to JsBin and paste your script in the left panel now your invalid hidden characters will be displayed as red dots, just remove those red dots and done, the code is now ready to use.

See this image :

To prevent it in future don't copy and paste the codes from JsFiddle or any other program that creates invalid hidden characters.

But there can be more reasons for this error and if you find one please tell us in comments.