Unexpected token ILLEGAL in webkit
Delete all invisible characters (whitespace) around that area, then give it another try.
I've seen that error in Safari when copy/pasting code. You can pick up some invalid (and unfortunately invisible) characters.
Used to happen to me a lot when copying from jsFiddle.
No visible cause for Unexpected token ILLEGAL
The error
When code is parsed by the JavaScript interpreter, it gets broken into pieces called "tokens". When a token cannot be classified into one of the four basic token types, it gets labelled "ILLEGAL" on most implementations, and this error is thrown.The same error is raised if, for example, you try to run a js file with a rogue @
character, a misplaced curly brace, bracket, "smart quotes", single quotes not enclosed properly (e.g. this.run('dev1)
) and so on.
A lot of different situations can cause this error. But if you don't have any obvious syntax error or illegal character, it may be caused by an invisible illegal character. That's what this answer is about.
But I can't see anything illegal!
There is an invisible character in the code, right after the semicolon. It's the UnicodeU+200B
Zero-width space character (a.k.a. ZWSP
, HTML entity
). That character is known to cause the Unexpected token ILLEGAL
JavaScript syntax error.And where did it come from?
I can't tell for sure, but my bet is on jsfiddle. If you paste code from there, it's very likely to include one or moreU+200B
characters. It seems the tool uses that character to control word-wrapping on long strings.UPDATE 2013-01-07It's also been reported that code pasted from the Chrome developer tools may include that character, but I was unable to reproduce that with the current version (22.0.1229.79 on OSX).After the latest jsfiddle update, it's now showing the character as a red dot like codepen does. Apparently, it's also not inserting
U+200B
characters on its own anymore, so this problem should be less frequent from now on.UPDATE 2015-03-17
Vagrant appears to sometimes cause this issue as well, due to a bug in VirtualBox. The solution, as per this blog post is to set
sendfile off;
in your nginx config, orEnableSendfile Off
if you use Apache.
How can I spot it?
The character is invisible, do how do we know it's there? You can ask your editor to show invisible characters. Most text editors have this feature. Vim, for example, displays them by default, and theZWSP
shows as <u200b>
. You can also debug it online: jsbin displays the character as a red dot on its code panes (but seems to remove it after saving and reloading the page). CodePen.io also displays it as a dot, and keeps it even after saving.Related problems
That character is not something bad, it can actually be quite useful. This example on Wikipedia demonstrates how it can be used to control where a long string should be wrapped to the next line. However, if you are unaware of the character's presence on your markup, it may become a problem. If you have it inside of a string (e.g., thenodeValue
of a DOM element that has no visible content), you might expect such string to be empty, when in fact it's not (even after applying String.trim
).ZWSP
can also cause extra whitespace to be displayed on an HTML page, for example when it's found between two <div>
elements (as seen on this question). This case is not even reproducible on jsfiddle, since the character is ignored there.
Another potential problem: if the web page's encoding is not recognized as UTF-8, the character may actually be displayed (as ​
in latin1, for example).
If ZWSP
is present on CSS code (inline code, or an external stylesheet), styles can also not be parsed properly, so some styles don't get applied (as seen on this question).
The ECMAScript Specification
I couldn't find any mention to that specific character on the ECMAScript Specification (versions 3 and 5.1). The current version mentions similar characters (U+200C
and U+200D
) on Section 7.1, which says they should be treated as IdentifierPart
s when "outside of comments, string literals, and regular expression literals". Those characters may, for example, be part of a variable name (and var x\u200c;
indeed works).Section 7.2 lists the valid White space characters (such as tab, space, no-break space, etc.), and vaguely mentions that any other Unicode “space separator” (category “Zs”) should be treated as white space. I'm probably not the best person to discuss the specs in this regard, but it seems to me that U+200B
should be considered white space according to that, when in fact the implementations (at least Chrome and Firefox) appear to treat them as an unexpected token (or part of one), causing the syntax error.
Uncaught SyntaxError: Unexpected token ILLEGAL
You probably have embedded illegal characters. Look in your JS with a hex editor and look for any characters that aren't visible ASCII characters. They may be getting stripped off by JSfiddle
See Chrome Uncaught Syntax Error: Unexpected Token ILLEGAL
Uncaught SyntaxError: Unexpected token ILLEGAL for Chrome Browser
There is an invisible character following the last });
of your last line. When I pasted it into my editor, it appeared as a .
.
View your code in an editor capable of displaying non-printable characters with some kind of symbol, or view it in a hex editor.
Unexpected token ILLEGAL (ELF) Raspberry Pi Node Webkit Linux v0.31.3
The reason why it is not working is because you are trying to run NW.js compiled for x86 (and x64) platforms but Raspberry Pi uses ARM architecture.
You can find a NW.js binary that will work in all Raspberry Pi versions out of the box here: https://github.com/jalbam/nwjs_rpi
Uncaught SyntaxError: Unexpected token ILLEGAL
If you want to assign a string that spans multiple lines - use \ string continuation:
var leftMiddleJS = 'function onmouseover() { \
document.getElementById("background").style.marginTop = "-135px"; \
document.getElementById("background").style.marginLeft = "0px"; \
document.getElementById("background").style.width = "760px"; \
document.getElementById("background").style.height = "520px"; \
document.getElementById("background").style.marginTop = "0"; \
document.getElementById("background").style.marginLeft = "0"; \
\
function click() { \
document.getElementById("outsideContainer").style.marginTop = "0"; \
document.getElementById("outsideContainer").style.marginLeft = "0"; \
document.getElementById("outsideContainer").style.width = "300px"; \
document.getElementById("outsideContainer").style.height = "250px"; \
document.getElementById("insideContainer").style.marginTop = "-135px"; \
document.getElementById("insideContainer").style.marginLeft = "0px"; }';
SyntaxError: Unexpected token '.' after trying to load an angular website
I remembered, that i once had the same problem trying to open an ionic app. I fixed the problem by lowering the target
version in my tsconfig.json
file:
In my case i had to go from es2020
to es2018
to make it work, since i have installed an older ios version (13.3.1) on my test device.
I hope it helps some of you!
Related Topics
Google Map API - Multiple Icons in Wrong Spot
How to Access Nested JSON Data
Passing a PHP Variable to JavaScript in a Blade Template
Object Doesn't Support This Property or Method Rails Windows 64Bit
Single Quote Escape in JavaScript Function Parameters
Why Is Usestate Not Triggering Re-Render
Javascript: How to Detect If a Word Is Highlighted
Check If Array Contains All Elements of Another Array
What Is the JavaScript Mime Type for the Type Attribute of a Script Tag
How to Filter an Array in JavaScript
Create Regexps on the Fly Using String Variables
How to Achieve Dynamic Scoping in JavaScript Without Resorting to Eval
Invoke a Callback at the End of a Transition
What's Wrong with Var X = New Array();
Check If Element Is Visible on Screen