Editor’s note: This article is part of our Code Optimization series, where we take a look at how to optimize coding for better efficiency in a bid to be better coders.
According to its website, the two most common ways the JSHint tool is used are as the command line tool and the API. Let’s take a look at how you can download use both, along with other linting optiosn the tools provide.
Via Command Line Tool
(1) If you haven’t have Node.js installed in your computer, then you’ll have to go its website and download and install it first. To check if Node.js has been successfully installed you can run the command
npm -version in the command-line interface (CLI) and it’ll show you the version of the Node.js in your computer (or you can just run the command
npm and see what happens).
Read more: Beginner’s Guide to Node.js
(2) To install the JSHint tool, run the command
npm install jshint in CLI. If you want to check if JSHint has been successfully installed, run the command
jshint -version to see its version. Once this step is over, installation is complete.
(1) Download the compressed file from this GitHub link, and unzip it. In the dist folder you’ll find the jshint JS file (the API library).
source array and display the analysis results on the page.
(3) We passed the
options object containing linting options (we’ll go into options shortly) as parameters to the
JSHINT function. The analysis result (a JSON object) is fetched from
JSHINT‘s function property called
JSON.stringify is used for display-only here, to display the result returned from the
data function in string format on the page. The beautified JSON string looks like this. The highlighted parts are the errors found by JSHint worded in simple sentences.
Linting options let us configure the linting process. We can specify which type of errors or wanring need to be linted and which don’t. In the previous example two linting options were used namely
undef option flags undeclared variables, and
unused will flag variables that were declared but never used. Like these there are many more options that you can see a list of in this page, if you want to search an option, there’s a search bar provided at the top right corner.
If you’re using the CLI tool via Node.js, you can write the linting options inside a
package.json file under the property
// -- test.js -- /* jshint undef: true, unused: true */ foo(); a = 7;
There are more ways to configure linting options in your project based on the tool you’re using. Check out the different ways for configuration here.