"Unexpected token ILLEGAL": A Hidden Culprit
Despite its straightforward appearance, the code that caused the "Unexpected token ILLEGAL" error contains an invisible character that is the actual culprit. This invisible character is the Unicode U 200B Zero-width space character (ZWSP).
Invisible Character Origin
The source of the ZWSP character is often unknown, but it is commonly found in code pasted from jsfiddle or the Chrome developer tools. In jsfiddle, the character is used to control word-wrapping.
Identifying the Invisible Character
Although invisible, the ZWSP character can be detected by enabling the display of invisible characters in your code editor or using online debugging tools. Editors like Vim display the character as
Effects and Related Problems
The ZWSP character can cause various problems, including:
ECMAScript Specification
The ECMAScript Specification does not explicitly mention the U 200B character as an unexpected token. However, it suggests that similar characters (U 200C and U 200D) should be treated as part of identifiers when not within certain code elements. The behavior of implementations (Chrome and Firefox) in treating the ZWSP as an unexpected token appears inconsistent with the specification.
The above is the detailed content of Why Does My JavaScript Code Throw an 'Unexpected token ILLEGAL' Error?. For more information, please follow other related articles on the PHP Chinese website!