How to Fix "illegal character u+" Errors in JavaScript

Ferenc Almasi β€’ 2022 July 12 β€’ πŸ“– 2 min read

The "Uncaught Syntaxerror: Illegal character u+" error occurs, whenever JavaScript comes across an invalid character in your code. In the worst-case scenario, these characters may be invisible making them almost impossible to spot. Luckily, most modern IDE or online editors can pick them up. Take a look at the below example:

// Copy and paste me to your console
'Hello World! πŸ‘‹'​
Copied to clipboard!

It looks like valid code, but there is an invisible character at the end of the string, which can cause the above error. Now try to paste the same code into JsFiddle or VSCode. These editors will highlight the invisible character so you will know exactly where to look.

In order to fix the error, simply remove the invalid character by deleting it. The same error can also happen if similar, but not the same characters are used for operators or reserved characters. Take a look at another code examples:

// Uses invalid quotations. Notice the difference between β€œβ€, and ""
β€œHello World! πŸ‘‹β€

// Uses –, instead of - 
1 – 1

// Using ΝΎ, instead of ; which looks exactly the same, but they are different characters
"Hello World! πŸ‘‹"ΝΎ
Copied to clipboard!

All of these contain invalid characters, even though they might be hard to spot at first. The first example is also incorrectly highlighted since it is not a string. The second may also be visible to some, but the third is indistinguishable from a real semicolon.

This is also noted in VSCode: " The characterΒ U+037eΒ "ΝΎ" could be confused with the characterΒ U+003bΒ ";", which is more common in source code".

If your IDE does not highlight anything, you can try to paste the code into an online editor such as JsFiddle, or just completely delete the line that breaks, and retype it.

Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Unlimited access to hundred of tutorials
  • check Access to exclusive interactive lessons
  • check Remove ads to learn without distractions
Become a Pro