Fork me on GitHub

JSLint Error Explanations

JSLint will hurt your feelings. It's time to make them better!

Unclosed string

When do I get this error?

JSLint and JSHint will throw the "Unclosed string" error when they encounter a string that is not closed at the next line break, or at the end of the program. In this first example, we accidentally forget to close our string:

In the next example, we want our string to include a backslash character. The string appears to be closed but actually isn't, due to the backslash character escaping the closing quote:

And this final example, which is only valid in ECMAScript 5-supporting environments, features a string that has not closed by the end of the program (the previous two examples failed at the first line break):

Why do I get this error?

This error is raised to highlight a JavaScript syntax error. Your code will not run unless you fix this issue. The ECMAScript grammar states that any string literal must be closed by the same character (either " or ') that opened it (ES5 §7.8.4):

StringLiteral ::
    " DoubleStringCharactersopt "
    ' SingleStringCharactersopt '

To fix the error, simply close any unclosed strings:

The second example above failed because the backslash character was escaping the closing quote, turning it into a literal character rather than a syntactic structure. To include a backslash in a string, you need to escape the backslash itself:

Finally, notice that in the third example above, an error is thrown even though we have reached the end of the program. Since semi-colons can be automatically inserted by the JavaScript engine, you may be forgiven for thinking that this example would not fail when taken as an entire program in its own right. However, as the grammar quoted above shows, all strings have to be closed, so in this case we simply need to close the string again:

In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. Since this message relates to a fatal syntax error you cannot disable it.

James Allardice

This article was written by James Allardice, an enthusiastic young JavaScript developer at Global Personals (we're looking for developers so please apply). He is passionate about writing clean, maintainable JavaScript and uses JSHint every day to help achieve this.

You can follow him on Twitter, fork him on GitHub and find him on Google+.

Proceeds generated by this site are donated to help advance other open source projects

comments powered by Disqus