JSHint is a program that flags suspicious usage in programs written in JavaScript. The core project consists of a library itself as well as a CLI program distributed as a Node module.
More docs: List of all JSHint options · CLI flags · Writing your own reporter · FAQ
The easiest way to use JSHint is to install it as a Node program. To do so, simply run the following command in your terminal (flag -g installs JSHint globally on your system, omit it if you want to install JSHint in the current working directory):
$ npm install jshint -g
After you've done that you should be able to use the jshint
program. The simplest use case would be linting a single file or all JavaScript files in a directory:
$ jshint myfile.js
myfile.js: line 10, col 39, Octal literals are not allowed in strict mode.
1 error
If a file path is a dash (-
) then JSHint will read from standard input.
JSHint comes with a default set of warnings but it was designed to be very configurable. There are three main ways to configure your copy of JSHint: you can either specify the configuration file manually via the --config
flag, use a special file .jshintrc
or put your config into your projects package.json
file under the jshintConfig
property. In case of .jshintrc
, JSHint will start looking for this file in the same directory as the file that's being linted. If not found, it will move one level up the directory tree all the way up to the filesystem root. (Note that if the input comes from stdin, JSHint doesn't attempt to find a configuration file)
This setup allows you to have different configuration files per project. Place your file into the project root directory and, as long as you run JSHint from anywhere within your project directory tree, the same configuration file will be used.
Configuration file is a simple JSON file that specifies which JSHint options to turn on or off. For example, the following file will enable warnings about undefined and unused variables and tell JSHint about a global variable named MY_GLOBAL
.
{
"undef": true,
"unused": true,
"predef": [ "MY_GLOBAL" ]
}
In addition to using configuration files you can configure JSHint from within your files using special comments. These comments start either with jshint
or global
and are followed by a comma-separated list of values. For example, the following snippet will enable warnings about undefined and unused variables and tell JSHint about a global variable named MY_GLOBAL
.
/* jshint undef: true, unused: true */
/* global MY_GLOBAL */
You can use both multi- and single-line comments to configure JSHint. These comments are function scoped meaning that if you put them inside a function they will affect only this function's code.
Here's a list of configuration directives supported by JSHint:
A directive for setting JSHint options.
/* jshint strict: true */
A directive for setting JSHint-compatible JSLint options.
/* jslint vars: true */
A directive for telling JSHint about global variables that are defined elsewhere. If value isfalse
(default), JSHint will consider that variable as read-only. Use it together with theundef
option.
/* global MY_LIB: false */
You can also blacklist certain global variables to make sure they are not used anywhere in the current file.
/* global -BAD_LIB */
A directive for telling JSHint about global variables that are defined in the current file but used elsewhere. Use it together with the unused
option.
/* exported EXPORTED_LIB */
A directive for telling JSHint about all properties you intend to use. This directive is deprecated.
A directive for telling JSHint to ignore a block of code.
// Code here will be linted with JSHint.
/* jshint ignore:start */
// Code here will be linted with ignored by JSHint.
/* jshint ignore:end */
All code in between ignore:start
and ignore:end
won't be passed to JSHint so you can use any language extension such as Facebook React. Additionally, you can ignore a single line with a trailing comment:
ignoreThis(); // jshint ignore:line
Most often, when you need to tune JSHint to your own taste, all you need to do is to find an appropriate option. Trying to figure out how JSHint options work can be confusing and frustrating (and we're working on fixing that!) so please read the following couple of paragraphs carefully.
JSHint has two types of options: enforcing and relaxing. The former are used to make JSHint more strict while the latter are used to suppress some warnings. Take the following code as an example:
function main(a, b) {
return a == null;
}
This code will produce the following warning when run with default JSHint options:
line 2, col 14, Use '===' to compare with 'null'.
Let's say that you know what you're doing and want to disable the produced warning but, in the same time, you're curious whether you have any variables that were defined but never used. What you need to do, in this case, is to enable two options: one relaxing that will suppress the === null
warning and one enforcing that will enable checks for unused variables. In your case these options are unused
and eqnull
.
/*jshint unused:true, eqnull:true */
function main(a, b) {
return a == null;
}
After that, JSHint will produce the following warning while linting this example code:
demo.js: line 2, col 14, 'main' is defined but never used.
demo.js: line 2, col 19, 'b' is defined but never used.
Sometimes JSHint doesn't have an appropriate option that disables some particular warning. In this case you can use jshint
directive to disable warnings by their code. Let's say that you have a file that was created by combining multiple different files into one:
"use strict";
/* ... */
// From another file
function b() {
"use strict";
/* ... */
}
This code will trigger a warning about an unnecessary directive in function b
. JSHint sees that there's already a global "use strict" directive and informs you that all other directives are redundant. But you don't want to strip out these directives since the file was auto-generated. The solution is to run JSHint with a flag --verbose
and note the warning code (W034 in this case):
$ jshint --verbose myfile.js
myfile.js: line 6, col 3, Unnecessary directive "use strict". (W034)
Then, to hide this warning, just add the following snippet to your file:
/* jshint -W034 */
A couple things to note:
W
), it doesn't work with errors (code starts with E
).To re-enable a warning that has been disabled with the above snippet you can use:
/* jshint +W034 */
This is especially useful when you have code which causes a warning but that you know is safe in the context. In these cases you can disable the warning as above and then re-enable the warning afterwards:
var y = Object.create(null);
// ...
/*jshint -W089 */
for (var prop in y) {
// ...
}
/*jshint +W089 */
This page contains a list of all options supported by JSHint.
By default JSHint warns when you omit break
or return
statements within switch statements:
switch (cond) {
case "one":
doSomething(); // JSHint will warn about missing 'break' here.
case "two":
doSomethingElse();
}
If you really know what you're doing you can tell JSHint that you intended the case block to fall through by adding a /* falls through */
comment:
switch (cond) {
case "one":
doSomething();
/* falls through */
case "two":
doSomethingElse();
}
[JSLint] and the popular alternatives [JSHint] and [ESLint] are brilliant tools that make your code more consistent. But sometimes it's hard to understand exactly what they're asking you to change. JSLint goes so far as to be proud of the fact that it will "hurt your feelings".
JSLint Error Explanations is designed to help you improve your JavaScript by understanding the sometimes cryptic error messages produced by JSLint, JSHint and ESLint, and teaching you how to avoid such errors.
The "{a} is already defined" error is thrown when JSLint or JSHint encounters a declaration with an identifier that has been used in a previous declaration. This applies to both variab…
Also known as
This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint and JSHint prior to version 1.0.…
Also known as
The "Avoid arguments.{a}" error is thrown when JSLint, JSHint or ESLint encounters a reference to the callee or caller property of an arguments object. The text of this warning can the…
Also known as
This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint and JSHint the warning given has…
Also known as
The "Bad constructor" error is thrown when JSLint or JSHint encounters the new operator followed by a literal value. In the following example we are attempting to apply the new operato…
Also known as
This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint and JSHint prior to version 2.1.…
Also known as
This warning has existed in several forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint and JSHint prior to version…
Also known as
The "Value of '{a}' may be overwritten in IE8 and earlier" error is thrown when JSHint or ESLint encounters a try...catch statement in which the catch identifier is the same as…
Also known as
This warning has existed in two forms in JSLint and JSHint. It was introduced in JSLint in June 2011 and has remained in both tools ever since. In JSLint the warning given is "Combine this with the previous…
Also known as
This warning has existed in a few forms in both JSLint and JSHint. It was introduced in the original version of JSLint and has remained in both tools ever since. In JSHint prior to version 1.0.0 the warning give…
Also known as
This warning has existed in a few forms in both JSLint and JSHint. It was introduced in the original version of JSLint and has remained in both tools ever since. In JSHint prior to version 1.0.0 the warning give…
Also known as
The "const '{a}' has already been declared" error is thrown when JSHint encounters a constant declaration with an identifier that has already been used in a previous constant d…
Also known as
The "Unexpected dangling '_' in '{a}'" error is thrown when JSLint, JSHint or ESLint encounters an identifier that begins or ends with the underscore character. JSHint…
Also known as
This warning has existed in various forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three linters ever since. In JSLint the warning given is "…
Also known as
The "['{a}'] is better written in dot notation" error is thrown when JSLint, JSHint or ESLint encounters an attempt to access a property using a string literal within a pair of…
Also known as
This warning has existed in three forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three linters ever since. In JSLint the warning given is the gene…
Also known as
The "Empty class" error is thrown when JSLint, JSHint (only versions before 1.0.0) or ESLint encounters a regular expression literal containing an empty character class. The following…
This is one of many generic error messages uses by JSLint in a number of situations. Most of these cases are covered in detail by dedicated articles. Following is a list of situations that will…
The "ES5 option is now set per default" error is thrown when JSHint (version 2.0.0 and above only) encounters the es5 option with a value of true. Here's an example in which we set…
Also known as
This warning has existed in various forms across the three main linters. It was introduced in the original version of JSHint and has remained (in a way) in all three tools from some point since. In JSLint the wa…
Also known as
This warning has existed in two forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint the warning given is "eval i…
Also known as
The "Do not assign to the exception parameter" error is thrown when JSLint, JSHint or ESLint encounters an assignment inside a catch block to the identifer associated with that block.…
Also known as
The "Expected exactly one space between '{a}' and '{b}'" error is thrown when JSLint encounters a number of spaces that is not equal to one in the following situations…
The "Expected a string and instead saw '{a}'" error is thrown when JSLint encounters a comparison operator in which one of the operands is a typeof expression and the other ope…
The "Expected parameter (value) in set '{a}' function" error is thrown when JSLint encounters property setter function in which the first parameter is not named value. In the f…
The "Extending prototype of native object: '{a}'" error, and the alternative "{a} prototype is read only, properties should not be added" error, is thrown when JSHint…
Also known as
This warning has existed in various forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint the warning given is the gen…
Also known as
The "The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype" error is thrown when JSLint encounters a for-in statement in which the…
Also known as
This warning has existed in two forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three linters ever since. In JSLint and JSHint prior to version 1.0…
Also known as
The "Function statements should not be placed in blocks" error (and the alternative "Function declarations should not be placed in blocks" error) is thrown when JSLint or JSH…
Also known as
The "Don't make functions within a loop" error is thrown when JSLint, JSHint and ESLint encounter a function expression in a for, while or do statement body. In the following examp…
Also known as
The "Use the function form of 'use strict'" error is thrown when JSLint, JSHint or ESLint encounters a strict mode directive in the outermost scope of the code. In the followin…
Also known as
This warning has existed in two forms across JSLint and JSHint. It was introduced in May 2011 version of JSLint and remained in both tools for a period of time. In JSLint between May 2011 and August 2013 the mes…
Also known as
The "Option 'validthis' can't be used in a global scope" error is thrown when JSHint encounters the validthis option in a global scope. Here's a silly example in which…
Also known as
The "'hasOwnProperty' is a really bad name" error is thrown when JSHint encounters an assignment to an object property with the identifier hasOwnProperty. This applies to both…
Also known as
This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint and JSHint prior to version 1.0.…
Also known as
The "It is not necessary to initialize '{a}' to 'undefined'" error is thrown when JSLint, JSHint or ESLint encounters a variable statement in which the variable is expl…
Also known as
The "Invalid typeof value '{a}'" error is thrown when JSHint encounters a comparison with a typeof expression on one side and an invalid string literal on the other. In the fol…
Also known as
The "Function statements are not invocable. Wrap the whole function invocation in parens" error (and the alternative "Function declarations are not invocable" error) is throw…
Also known as
This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint versions dated before May 2013 t…
Also known as
The "Do not use {a} as a constructor" error is thrown when JSLint, JSHint or ESLint encounters a call to String, Number, Boolean, Math or JSON preceded by the new operator. In the foll…
Also known as
This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint the warning given is the generic…
Also known as
The "Missing radix parameter" error is thrown when JSLint, JSHint or ESLint encounters a call to the parseInt function that only has one argument. As of JSHint 2.3.0 the warning will o…
Also known as
The "Missing 'use strict' statement" error is thrown when JSLint, JSHint and ESLint encounter a function that does not contain the strict mode directive, and none of whose ance…
Also known as
The "Mixed double and single quotes" error is thrown when JSHint encounters string literal delimited by double or single quote characters when a string literal delimited by the other h…
Also known as
The "Move the invocation into the parens that contain the function" error is thrown when JSLint and ESLint encounter an immediately invoked function expression in which the invoking pa…
The "Move 'var' declarations to the top of the function" error is thrown when JSLint encounters a variable declaration in a for or for-in statement initialiser. Here's an e…
The "Nested comment" error is thrown when JSLint encounters the character sequence /* inside a multiline comment. Here's an example: /* This is a multiline comment. /* It's…
The "Do not use 'new' for side effects" error is thrown when JSLint, JSHint or ESLint encounters a function invocation preceded by the new operator when not part of an assignme…
Also known as
The "'{a}' is not a function" error is thrown when JSLint, JSHint or ESLint encounters an attempt to invoke the Math object as a function. JSLint and ESLint (but not JSHint) wi…
Also known as
This warning has existed in three forms across the three main linters. It was introduced in the original version of JSLint and has remained (in a way) in all three tools ever since. In JSLint the warning given i…
Also known as
This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint the warning given is "'…
Also known as
This warning has existed in various forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since. In JSLint versions dated May 2013 onwa…
Also known as
This warning has existed in three forms across the three main linters. It was introduced in the original version of JSLint and has remained (in a way) in all three tools ever since. In JSLint the warning given i…
Also known as