Writing Better JavaScript

JavaScript has exploded as a programming language in recent times. From using it in Appcelerator to build platform independent applications to fully dependable services like MongoDB and Node.js, JavaScript is seeing a large increase in more then just the browser.

How do you write better JavaScript code? Several things come to mind.

Keep It Local

JavaScript has something called implicit global scope. Failure to prefix variable declarations with var will lead to a globally available variable. How does this impact your code? Polluting the global scope can cause many issues, chief among them is interference with other JavaScript libraries, specifically those that are poorly written and have scope issues. If you roll all your own JavaScript then it is doubtful you will see immediate issues, however, the errors and unexpected behavior that can arise from this is difficult to debug. It is much easier to simply keep in the current scope in mind and use var.

Use A Library

Web browsers use different JavaScript engines. Firefox uses SpiderMonkey (with the improvements of TraceMonkey in 3.5 and JägerMonkey in the 4 betas), Safari uses Nitro, Google’s Chrome and open source variant Chromium both use V8. Internet Explorer is due out with a dedicated JavaScript engine in IE9. These different engines have different sets of standard browser implementations. Using a JavaScript library like jQuery can often improve development time and lessen the amount of cross browser issues you encounter.

Be Consistent

It’s better to be consistently wrong then haphazardly right. There are nuances of JavaScript that are often either unknown or overlooked that can lead to issues. Use a quality checker (I recommend JSLint) to ensure that all your code is consistent with the framing “good practices” of JavaScript.

Namespace

Java developers will often write company or library specific packages for easy import and use in other applications. com.itzik.utils and the like. The exact same thing can be done in JavaScript and it highly recommended. If you’re writing a library or designing a reusable set of objects, put them into a namespace object. This way everything is centralized and the global scope is polluted as little as possible. Unobtrusive JavaScript is the approach you are aiming for.

written December 10th, 2010

December 2010

Can’t find what you’re looking for? Try hitting the home page or viewing all archives.