JS Tip of the Day: Detect Errors Early With Linting

Detect Errors Early With Linting
Level: Intermediate

As an interpreted language, you don’t need to run JavaScript through a compiler in order to execute it. You provide a runtime (e.g. a browser or Node) with source code and that runtime handles the necessary parsing and conversions needed to run it as a program. This means there’s nothing in between you writing the code and the code being run to let you know if there are any errors - something a compiler can be useful for. That’s where a linter can come in handy.

A linter uses static analysis to look for errors in code without having to run the code. It can identify syntax errors, improper usage, and even warn you of possible issues you might encounter at runtime. It won’t catch all bugs, but goes a long way to help you out, especially with the simpler things.

A popular JavaScript linter is ESLint (short for ECMAScript Lint). It can be run standalone or, more conveniently, integrated with your code editor to show you errors as you type.

To see a live demo of ESLint in action, head over to eslint.org/demo.

var foo = bar;
// 1:5 - 'foo' is assigned a value but never used.
// 1:11 - 'bar' is not defined.

Even if you’re an experienced programmer, having a linter set up for your project can be extremely helpful. And if you’re not, it can be a great guide for helping you learn.

More info: