Do you validate your JavaScript code?
Writing about whether people validate their CSS, it made me wonder about how/if people ensure the quality of their JavaScript code.
When I wrote about validating CSS, some of the feedback was that if you validate/promote valid HTML, naturally you should validate your CSS. With that sentiment, I’d argue that it’s as, if not more, important to also validate your JavaScript code (if you don’t agree, skip to the question at the end of this post, and give me a reply to that).
Enter JSLint
In difference to HTML and CSS, W3C doesn’t offer any way to validate your JavaScript code. However, JavaScript Jedi Master Douglas Crockford, founder of JSON, JSMin and lots of other work in the JavaScript filed has created the JSLint - The JavaScript Verifier.
You can read more about it in the JSLint documentation, but briefly put, it’s a great tool for verifying the syntax as well as best practices in your JavaScript code. As Douglas himself usually puts it to people using it for the first time:
It will hurt your feelings
While he might be right in some cases, I must say that since I started using it, my JavaScript code has gotten a lot better and I’m able to avoid both plain syntax errors as well as runtime caveats that many of us stumble onto way too often.
JSLint settings
When you first enter the JSLint validation page, there are a few checkboxes to check in the Options box I’d like to recommend to anyone getting confused:
- Assume a browser
- Disallow undefined variables
- Disallow == and !=
External dependencies
If your code in the specific file you’re validating, it’s easy to specify those dependencies to avoid having throw an error. Just enter this code at the top of your JavaScript file (naturally, replace the names below with theobjects/methods you use):
/*global DOMAssistant, $, $$ */
JSLint web development environment integrations
Some people prefer to navigate to the JSLint page, paste in your code, and validate it, while other people feel they become faster if they can do it natively in their preferred development environment. Therefore, I’d like to offer links to tools enabling that for you:
- JavaScript Tools TextMate bundle
- Verifying Javascript files with JSLint from Eclipse IDE
- JSLint.VS - JavaScript Verifier for Visual Studio
Do you use JavaScript validation?
So, we’ve been through HTML and CSS validation, which leads us to the inevitable question:
Do you validate your JavaScript code?






