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

Ferenc Almasi β€’ 2022 July 12 β€’ Read time 2 min read
  • twitter
  • facebook
JavaScript

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:

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

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:

Copied to clipboard! Playground
// 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! πŸ‘‹"ΝΎ

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.

  • twitter
  • facebook
JavaScript
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.