JavaScript: Acknowledge Its Shortcomings and Hack Like a Legend

JavaScript is the world’s most popular programming language. It’s easy to learn, lightweight and extendable.

It’s also the punch line of many a (nerdy) joke given its well-documented design flaws.

But we’re smart. We tweak, rewrite and hack our way around bugs and incompatibilities. We’re also lucky enough to work in an industry where the best knowledge is typically free: our open-source, share-what-you-know approach makes learning easy.

We’ve tapped into some of the brightest JavaScript programmers to find out what they find indispensable for its programming. Here’s their short list:

Avoid Memory Leaks

Traditional “memory” is not a JavaScript problem, but when the browser doesn’t release unneeded objects, the site can drag. Fortunately, Google’s open source tool can help you find these leaks.

Watch for code like this, where “e” references “e and d” even though it uses only “d” and where “e” also references closure, resulting in a circular reference:

function foo(e,d) {
    $(e).on("click", function() {
        //Do something with d

Use instead:

function foo(e, d) {
    $(e).on("click", cbk(d));

function cbk (d) {

Minimize Code in Loops to Gain Speed

Developers accustomed to advanced programming can run into trouble writing JavaScript when they make loops more complicated than JavaScript’s simplicity allows. Too much in a loop can slow things down because it may be more than each iteration can handle, locking up the browser. Nicholas Zakas, a front-end developer for Box and former front-end engineer for Yahoo’s home page, offers some helpful tips on how to troubleshoot a long-running script dialog.

For example, the control condition and control variable are not combined in the “for” loop illustrated below:

For ( var x = 0; x < 10; x++ ) {

Using a “while” loop instead of “for” flips the iteration around and solves the problem:

var x = 9;
do { } while( x-- );

Cache Objects

Caching your objects is a simple step that can eliminate repetitive results and make you more efficient. JavaScript Kit suggests that you store an often-accessed object in a user-defined variable, which saves time because it can be called to reference that object.

For example, this script repeatedly accesses “person.firstname”:

<script type="text/javascript">
for (i=0;i<person.firstname.length;i++)

The following revision goes straight to the containing array, saving time:

<script type="text/javascript">
var thename=person.firstname
for (i=0;i<thename.length;i++)

Use JavaScript Libraries

Libraries, like Jquery, are replacing traditional JavaScript coding standards. Since page load speed is a primary concern in today’s JavaScript, the SEO friendliness of Jquery or the interactive richness of YUI makes them worth learning. Using JavaScript libraries not only improves the code’s readability but also decreases front-end development time, and there are libraries available for nearly any application you need, from fonts and animation to debugging and forms.

Use Each Other

In JavaScript’s infancy, no one expected it to do much more than change background color by accessing CSS properties and enhancing the page. In the ’90s, sloppy code-writing could yield an acceptable product. Now, good scripting is crucial because JavaScript regularly drives complex tasks. Well-thought-out, efficient code is essential, and there’s no better source than other expert programmers in the field.