Fork me on GitHub

JSLint Error Explanations

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


Unexpected label '{a}'

When do I get this error?

JSLint will throw the "Unexpected label '{a}'" error when it encounters a labelled statement whose statement is not an iteration or switch statement or a labelled statement in the global execution context. The following example contains a labelled var statement:

Why do I get this error?

This error occurs in two different situations. Both require their own explanation.

A labelled statement whose statement is not an iteration or switch statement

The error is raised in this situation because JSLint has detected a completely useless and potentially confusing piece of code. According to the ECMAScript standard, any statement can be labelled. However, since the language does not define a goto statement, labelled statements are only useful in conjunction with the break and continue statements, in the context of a loop or a switch.

The specification sums this up in section §12.12:

A Statement may be prefixed by a label. Labelled statements are only used in conjunction with labelled break and continue statements. ECMAScript has no goto statement.

Therefore, there is no reason at all to use a label unless it's labelling a for, while, do or switch statement.

A labelled statement in the global execution context

The error is raised in this situation because JSLint has detected a potentially confusing or dangerious piece of code. The same rules of the language (as detailed in the previous section) apply when the labelled statement occurs in the global execution context, but JSLint behaves slightly differently.

JSLint disallows the use of any labelled statements in the global execution context, whether they are iteration/switch statements or not. This is in contrast to the behaviour inside a function context, where labelled iteration and switch statements are allowed. The reasoning behind this is unclear, but it's probably simply because in languages that support a goto statement, labels in this sort of global scope would be very dangerous.

Since there is no way to refer to a label apart from inside an iteration or switch statement with the break or continue statement, and since every statement has its own label set, there is no way for a label in the global scope to interfere with other code (you cannot for example overwrite a label defined elsewhere in the scope by defining one with the same identifier).

For more detail, see this Stack Overflow question.


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