JSHint, A JavaScript Code Quality Tool
jshint.com
jshint.com
But I find I can't really convince anyone of it being useful, I remember being in an interview for a startup I read about on here and started talking about this as a tool I use (they asked "What tools do you use"). They basically brushed it off as "yeah, I don't think this is super useful, we probably have bigger problems than a missing semicolon".
As I understand it, general perception of people seems to be it's a tool for finding missing semicolons. I don't know how to give this a decent elevator pitch so other people can see the light.
http://altdevblogaday.com/2011/12/24/static-code-analysis/
While not all of this is immediately applicable to JSHint and the like, it provides some insight into why static code analysis can be seen as helpful.
As you also mention, it's really helpful for code within a codebase to be consistently written. JSHint can help enforce some of that consistency. This is where JSHint really shines.
If your JS is tied to some sort of "build" process (even a short 1-2 second one), catching errors before running the build is incredibly helpful. If you intend to minify your JS code (as you should) before distribution, finding missing semicolons can be very important, indeed. I find the most helpful JSHint errors to be about mistyped variable names. It's so much easier to see that in my editor, fix it, and THEN run it in the browser.
We have a Rails app with multiple JS files. One of them ended with a statement like var foo = { /* object contents / } / no semicolon here */
This worked in development but broke in production. Browsers seemed happy to assume the missing semicolon when the closing brace was followed by the end of the file. But in production, this JS file had other files concatenated to it, and it broke.
Lesson learned. We now use Syntastic to check our syntax in Vim, including Javascript.
I've run into something like this before:
for (var i = 0, len = items.length; i < len; i++)
{
var item = items[i];
// ...maybe some more code here...
var subItems = item.subItems;
for (var i = 0, len = subItems.length; i < len; i++) {
// whoops
}
}
The inner for loop's i & len variables are really the same as the outer for loop's i & len variables (there's no shadowing going on here because they're all in the same scope in JS). Depending on the order of things and how many subItems there are, you could end up with an infinite loop, iterating over the same items multiple times, or skipping certain items. It seems like it'd be an obvious problem to see, but if there's a significant amount of code above the inner for loop, you might miss it. It also seems like it'd result in very obvious bugs, but if subItems are rare, most of the time this code will run just fine.Most of the JS static analysis tools will complain about redeclaring i and len, which makes spotting this kind of problem much easier.
http://www.vim.org/scripts/script.php?script_id=3576
https://github.com/walm/jshint.vim
This has really helped catch some dumb errors and even make some nice recommendations.
My company recently implemented this as part of a git pre-commit hook so that we're less likely to have a silly syntax mistake cause problems.