Fork me on GitHub

JSLint Error Explanations

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


Missing 'use strict' statement

When do I get this error?

JSLint will throw the "Missing 'use strict' statement" error when it encounters a function that does not contain the strict mode directive, and none of whose ancestor scopes contain the strict mode directive. JSHint will throw this error in the same situation, but only when the strict option is set to true. Here's an example of a function that does not run in strict mode:

Why do I get this error?

The main reason this error is raised is to highlight a lack of convention. However, as JavaScript engines move forward, this error will increasingly be helpful as it should highlight areas of code that may not work as you expect them to, or may even cause fatal JavaScript errors.

A "use strict" statement is an example of a directive, which can appear as the first statement of a program or a function (ES5 §14.1):

A Directive Prologue is the longest sequence of ExpressionStatement productions occurring as the initial SourceElement productions of a Program or FunctionBody and where each ExpressionStatement in the sequence consists entirely of a StringLiteral token followed a semicolon. The semicolon may appear explicitly or may be inserted by automatic semicolon insertion. A Directive Prologue may be an empty sequence.

The Use Strict directive can be used to force the engine to conform to a strict subset of the language, as defined in ES5 Annex C. It has become something of a convention to run all JavaScript code in strict mode, to avoid falling into traps that are apparent in the non-strict language. See the previous link or the corresponding MDN article for the details of the differences in strict mode.

You can fix this error by simply adding a Use Strict directive to the function, or to an ancestor function:

In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. This message is treated as an error by JSHint which means you are unable to prevent it from being issued.


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