Monday, February 15, 2016

JS Hint

                   JsHint

JSHint is a tool that can help you to write more reliable and consistent JavaScript code. The tool works by checking your code for a number of common errors. If you’ve ever spent half an hour trying to debug your code only to find that you missed a comma somewhere, you’ll understand how useful JSHint can be.
As well as catching errors in your code, JSHint can be used to enforce coding conventions and style guides. This is really useful if you work on a team as it helps to keep your codebase consistent and easily readable.

Installing JSHint

JSHint is available through the node package manager (npm). If you don’t have npm installed, go to the node.js website and download a copy of node for your OS. This will install both node.js and npm.
Once you have npm installed, open up terminal and type:
npm install -g jshint
The -g flag means that jshint will be installed globally on your system. This allows you to access the executable from any directory.

Using JSHint on The Command Line

Now that you have JSHint installed let’s test a file. Here test file is called “demo.js”
window.onload = function() {  console.log('This is a JSHint demo') }
Open up terminal and navigate to the folder that contains your JavaScript file. Then run the following command to test the file:
jshint demo.js
If you used the demo.js file you should see two errors as shown below.




JSHint Errors
If JSHint doesn’t find any problems with your code nothing will be output into the terminal window. Open up the demo.js file in your favorite text editor and add the two missing semi-colons. Running JSHint again should now return no errors.
Running JSHint from the command line can become a bit tedious. Ideally you want to get real-time feedback about any errors as you write your code. There are a whole bunch of plugins available that can bring the goodness of JSHint directly into your favorite text editor. I have used intellij JsHint plugin for our Coupley project.

No comments:

Post a Comment