JSHint will throw the "'{a}' is defined but never used" error when it encounters a variable or function declaration, or function arguments, with an identifier that is not referred to in a visible scope, but only when the unused option is set to true. In the following example there are various unused identifiers:
This error is raised to highlight potentially useless code. Your code will run without error if you just ignore this warning, but it could be doing unnecessary work and could be confusing to other developers exposed to your code.
When you declare a variable or a function in JavaScript there is no obligation to use it later on. Therefore, code such as the example above is perfectly valid but also completely useless. If this warning relates to variables that are simply defined and never used, you can simply remove those variable declarations completely. However, sometimes you may declare variables in one file but use them in another. If that's the case, you can use the exported directive to tell JSHint about them:
In this example we have told JSHint that the identifiers x and example are used by external scripts so it no longer warns us about them.
If you switch the version of JSHint in the above example to 1.1.0, you should notice that there's now only a single warning instead of three. By default, JSHint 1.1.0 and above will only issue a warning if the last function parameter is unused. The reasoning behind this is explained by this issue (and expanded upon in this one). Basically, a common pattern, especially in Node.js, is to use the first parameter of a callback function as an error object, and the second as the data you are expecting. It is relatively common to simply ignore this error object, but you still need to identify it in the function signature if you want named access to the following arguments.
However, if you don't like this new default behaviour, don't worry. The unused option now accepts a value that configures how it works:
Option value | Description |
---|---|
vars | Only warn about unused variable and function declarations. Don't warn about any function parameters. |
last-param | Warn about unused variable and function declarations and the last parameter of functions. This is the default value if you set the option to true. |
strict | Warn about unused variable and function declarations and all parameters of functions. This is the equivalent to the pre-1.1.0 behaviour. |
Following that, here's another example in which we don't care about unused function parameters at all:
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 W098. This means you can tell JSHint to not issue this warning with the /*jshint -W098 */
directive.