Each editor is different so you will have to search on how to do that for your particular editor.Īnother option is to re-write the code by hand (no pasting) inside your own text editor. That should show you all of these special characters that would otherwise be invisible. Don't assume the code is valid just because it looks right.Įach time you copy something, paste it in your favorite text editor first and then enable an option to show all the non-printing characters. You just have to be more careful when copying code from another source. It is a nasty bug and can be very hard to spot.Īlso, if you see weird spaces/lines inside of your HTML document then it is most likely an issue with this invisible character, or with its cousin, the zero width space. So if somebody enters data into your JSON documents with this line separator then it will cause a syntax error once it is parsed into Javascript. JSON parsers allow the line separator to be used inside strings, but Javascript does not. What other problems can it cause?īesides the syntax error in your pages, it is possible to have even more problems with JSON APIs. You can even pick it up from a regular Notepad file or a Tweet.
You can pick it up from any source such as PDF files, Word documents, or any sort of medium that supports Unicode. You most likely copied a code from another website that contained this special character. The same thing will happen if you use a paragraph separator (U+2029). This makes it impossible to see in the text on a regular web page. It has no width so the browser does not display it. The line separator is a special character (code point U+2028 and HTML entity & #8232 ) that is used to separate lines instead using of a normal return. This invisible character is a line separator so it cannot be used in a string, hence the error message. In Javascript a string can contain anything as long as it is not a quote, a backslash or a line terminator. Why does it cause a problem in Javascript? You should get a "Synta圎rror: unexpected token ILLEGAL" message just like in the screen shot above. Copy the code below and paste it in your browser's dev console: var firstName = "Emilian !"
It is called a line separator and it can wreak havoc in your Javascript code. There is an invisible unicode character after the "n" and before the exclamation mark. Still nothing? I will tell you what is wrong. Take a couple more minutes and see if you can figure it out. See if you can spot the problem in this variable:ĭid you spot the problem? No? It is okay. I copied the text and saved it using different editors and still nothing: the error would follow me around from one editor to another. A couple of weeks back I ran into an issue where seemingly good Javascript code would return a syntax error.