These options tell JSHint to be more strict towards your code. Use them if you want to allow only a safe subset of JavaScript—very useful when your codebase is shared with a big number of developers with different skill levels.
| Name | Description |
|---|---|
bitwise |
This option prohibits the use of bitwise operators such as
|
curly |
This option requires you to always put curly braces around blocks in loops and conditionals. JavaScript allows you to omit curly braces when the block consists of only one statement, for example: while (day) shuffle(); However, in some circumstances, it can lead to bugs: while (day) shuffle(); sleep(); // You would think that this is a part of your loop, but it is not. Generally, unless you are careful, it is safer to require curly braces around all blocks. |
eqeqeq |
This options prohibits the use of Note: even if this option is off, JSHint will check for unsafe
comparisons like |
forin |
This option requires all for (key in obj) {
if (obj.hasOwnProperty(key)) {
// We are sure that obj[key] belongs to the object and was not inherited.
}
}
For more in-depth understanding of |
immed |
This option prohibits the use of immediate function invocations without wrapping them in parentheses. Wrapping parentheses assists readers of your code in understanding that the expression is the result of a function, and not the function itself. |
latedef |
This option prohibits the use of a variable before it was defined. JavaScript has function scope only and, in addition to that, all variables are always moved—or hoisted— to the top of the function. This behavior can lead to some very nasty bugs and that's why it is safer to always use variable only after they have been explicitly defined. For more in-depth understanding of scoping and hoisting in JavaScript, read JavaScript Scoping and Hoisting by Ben Cherry. |
newcap |
This option requires you to capitalize names of constructor functions.
Capitalizing functions that are intended to be used with Not doing so won't break your code in any browsers or environments but it will
be a bit harder to figure out—by reading the code—if the function was
supposed to be used with or without function MyConstructor() {
console.log(this);
}
new MyConstructor(); // -> [MyConstructor]
MyConstructor(); // -> [DOMWindow]
For more in-depth understanding on how |
noarg |
This option prohibits the use of |
noempty |
This options warns when you have an empty block in your code. JSLint was originally warning for all empty blocks and we simply made it optional. There were no studies reporting that empty blocks in JavaScript break your code in any way. |
nonew |
This option prohibits the use of constructor functions for side-effects. Some people like to call constructor functions without assigning its result to any variable: new MyConstructor(); There is no advantage in this approach over simply calling MyConstructor since
the object that the operator |
plusplus |
This option prohibits the use of unary increment and decrement operators. Some
people think that |
regexp |
This option prohibits the use of unsafe |
undef |
This option prohibits the use of explicitly undeclared variables. This option is very useful for spotting leaking and mistyped variables. /*jshint undef:true */
function test() {
var myVar = 'Hello, World';
console.log(myvar); // Oops, typoed here. JSHint with undef will complain
}
If your variable is defined in another file, you can use
|
strict |
This option requires all functions to run in EcmaScript 5's strict mode. Strict mode is a way to opt in to a restricted variant of JavaScript. Strict mode eliminates some JavaScript pitfalls that didn't cause errors by changing them to produce errors and it fixes mistakes that made it difficult for JavaScript engines to perform certain optimizations. Note: This option enables strict mode for function scope only, it prohibits the global scoped strict mode because it might break third-party widgets on your page. If you really want to use global strict mode, see the globalstrict option. |
trailing |
This option makes it an error to leave a trailing whitespace in your code. Trailing whitespaces can be source of nasty bugs with multi-line strings in JavaScript: // This otherwise perfectly valid string will error if // there is a whitespace after \ var str = "Hello \ World"; |
These options allow you to suppress certain types of warnings. Use them only if you are absolutely positive that you know what you are doing.
| Name | Description |
|---|---|
asi |
This option suppresses warnings about missing semicolons. There is a lot of FUD (fear, uncertainty and doubt) spread about semicolon spreaded by quite a few people in the community. The common myths are that semicolons are required all the time (they are not) and that they are unreliable. JavaScript has rules about semicolons which are followed by all browsers so it is up to you to decide whether you should or should not use semicolons in your code. For more information about semicolons in JavaScript read An Open Letter to JavaScript Leaders Regarding Semicolons by Isaac Schlueter and JavaScript Semicolon Insertion. |
boss |
This option suppresses warnings about the use of assignments in cases where
comparisons are expected. More often than not, code like for (var i = 0, person; person = people[i]; i++) {}
|
debug |
This option suppresses warnings about the |
eqnull |
This option suppresses warnings about |
es5 |
This option tells JSHint that your code uses ECMAScript 5 specific features such as getters and setters. Note that not all browsers implement these features. More info:
|
esnext |
This option tells JSHint that your code uses ES.next specific features such as
More info: |
evil |
This option suppresses warnings about the use of |
expr |
This option suppresses warnings about the use of expressions where normally you would expect to see assignments or function calls. Most of the time, such code is a typo. However, it is not forbidden by the spec and that's why this warning is optional. |
funcscope |
This option suppresses warnings about declaring variables inside of control structures while accessing them later from the outside. Even though JavaScript has only two real scopes—global and function—such practice leads to confusion among people new to the language and hard-to-debug bugs. This is way, by default, JSHint warns about variables that are used outside of their intended scope. function test() {
if (true) {
var x = 0;
}
x += 1; // Default: 'x' used out of scope.
// No warning when funcscope:true
}
|
globalstrict |
This option suppresses warnings about the use of global strict mode. Global strict mode can break third-party widgets so it is not recommended. For more info about strict mode see the |
iterator |
This option suppresses warnings about the |
lastsemic |
This option suppresses warnings about missing semicolons but only when the semicolon is omited for the last statement in a one-line block: var name = (function() { return 'Anton' }());
This is a very niche use case that is useful only when you use automatic JavaScript code generators. |
laxbreak |
This option suppresses most of the warnings about possibly unsafe line breakings in
your code. It doesn't suppress warnings about comma-first coding style. To suppress
those you have to use |
laxcomma |
This option suppresses warnings about comma-first coding style: var obj = {
name: 'Anton'
, handle: 'valueof'
, role: 'SW Engineer'
};
|
loopfunc |
This option suppresses warnings about functions inside of loops. Defining functions inside of loops can lead to bugs such as this one: var nums = [];
for (var i = 0; i < 10; i++) {
nums[i] = function (j) {
return i + j;
};
}
nums[0](2); // Prints 12 instead of 2
To fix the code above you need to copy the value of var nums = [];
for (var i = 0; i < 10; i++) {
(function (i) {
nums[i] = function (j) {
return i + j;
}
}(i));
}
|
multistr |
This option suppresses warnings about multi-line strings. Multi-line strings
can be dangerous in JavaScript because all hell breaks loose if you accidentally
put a whitespace in between the escape character ( Not that even though this option allows correct multi-line strings, it still warns about multi-line strings without escape characters or with anything in between the escape character and a whitespace. /*jshint multistr:true */
var text = "Hello\
World"; // All good.
text = "Hello
World"; // Warning, no escape character.
text = "Hello\
World"; // Warning, there is a space after \ (colored pink)
|
onecase |
This option suppresses watnings about switches with just one case. Most
of the time you want to use |
proto |
This option suppresses warnings about the |
regexdash |
This option suppresses warnings about unescaped |
scripturl |
This option suppresses warnings about the use of script-targeted
URLs—such as |
smarttabs |
This option suppresses warnings about mixed tabs and spaces when the latter are used for alignmnent only. The technique is called SmartTabs. |
shadow |
This option suppresses warnings about variable shadowing i.e. declaring a variable that had been already declared somewhere in the outer scope. |
sub |
This option suppresses warnings about using [] notation when it can be expressed
in dot notation: |
supernew |
This option suppresses warnings about "weird" constructions like
var singleton = new function() {
var privateVar;
this.publicMethod = function () {}
this.publicMethod2 = function () {}
};
|
validthis |
This option suppresses warnings about possible strict violations when
the code running in a strict mode and you use Note: This option can be used only inside of a function scope. JSHint will fail with an error if you will try to set this option globally. |
These options pre-define global variables that are exposed by popular JavaScript
libraries and runtime environments—such as browser or node.js. Essentially they
are shortcuts for explicit declarations like /*global $:false,
jQuery:false */.
| Name | Description |
|---|---|
browser |
This option defines globals exposed by modern browsers: all the way from good ol'
Note: this option
doesn't expose variables like |
couch |
This option defines globals exposed by CouchDB. CouchDB is a document-oriented database that can be queried and indexed in a MapReduce fashion using JavaScript. |
devel |
This option defines globals that are usually used for logging poor-man's
debugging: |
dojo |
This option defines globals exposed by the Dojo Toolkit. |
jquery |
This option defines globals exposed by the jQuery JavaScript library. |
mootools |
This option defines globals exposed by the MooTools JavaScript framework. |
node |
This option defines globals available when your code is running inside of Node runtime environment. Node.js is a server-side JavaScript environment that uses an asynchronous event-driven model. |
nonstandard |
This option defines non-standard but widely adopted globals such as
|
prototypejs |
This option defines globals exposed by the Prototype JavaScript framework. |
rhino |
This option defines globals available when your code is running inside of Rhino runtime environment. Rhino is an open-source implementation of JavaScript written entirely in Java. |
wsh |
This option defines globals available when your code is running as a script for the Windows Script Host. |
These options are legacy from JSLint. Aside from bug fixes they will not be improved in any way and might be removed at any point.
| Name | Description |
|---|---|
nomen |
This option disallows the use of dangling |
onevar |
This option allows only one x.nud = function () {
var b, f, i, j, p, seen = {}, t;
b = token.line !== nexttoken.line;
if (b) {
indent += option.indent;
if (nexttoken.from === indent + option.indent) {
indent += option.indent;
}
}
// [...]
};
|
passfail |
This option makes JSHint stop on the first error or warning. |
white |
This option make JSHint check your source code against Douglas Crockford's JavaScript coding style. Unfortunately, his “The Good Parts” book aside, the actual rules are not very well documented. |
JSHint, by JSHint Community.
Follow us on Twitter or GitHub. Maintained by Anton Kovalyov.