Fork me on GitHub

JSLint Error Explanations

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


'{a}' is a statement label

When do I get this error?

JSLint and JSHint will throw the "'{a}' is a statement label" error when it encounters a reference that shares an identifier with a label defined in the same scope. In the following example there is a variable declared in the global execution context with the identifier x. Inside the test function, there is a for statement with a label that also has the identifier x. JSLint and JSHint throw the error when we attempt to refer to the x variable in the outer scope:

Why do I get this error?

This error is raised to help improve the readability of your code. It may be confusing to others (and to you, if you revisit your code some time in the future) to have various references using the same identifier but to refer to completely different things.

However, the code in the example above is valid and will not cause problems in any environment. Read on if you would like to understand the actual behaviour of the code.

The ECMAScript 5 specification states the following (section §12.12):

The production Identifier : Statement is evaluated by adding Identifier to the label set of Statement and then evaluating Statement.

Every statement has a label set (it's empty by default for most statements, except loops and switch statements which have an implicit label so you can break out of them). When you give a statement a label, the identifier of that label is added to the label set. The label set is completely separate from the variable environment of the scope, so there is no potential for naming conflicts between label identifiers and variable identifiers. Therefore, this JSLint and JSHint error is designed purely to make your code clearer and easier to follow.

In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. The identifier of this warning is W037. This means you can tell JSHint to not issue this warning with the /*jshint -W037 */ directive.


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