How to Fix "illegal character u+" Errors in 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:
// 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:
// 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.
Rocket Launch Your Career
Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies: