I’m noticing more and more people tend to favor omitting semicolons when writing JavaScript.
Now, my opinion concerning these matters is simple: the language may allow it, but it’s dangerous and will lead to problems. Eventually.

The same kind of reasoning actually goes for the placement of curly braces. I tend to always have them on the same line, just for consistency.
Let me give you a few examples why.

(function () {
    return
    {
        a: 5
    };
}());

// the code above actually translates to:

(function () {
    return; // ES grammar says return cannot be followed by a newline
            // so semicolon insertion "fixes" it
    {
        a: 5; // this labelled statement (yes, really) gets a semicolon as well
    };
}());
var a = function (b) {
    alert(b);
} // no semicolon here

(function () {
    // blah blah, do stuff here

    return 5; // just because.
}());

// because I didn't pay attention, the above code alerts 5. Now, let's see what's going on:

var a = function (b) {
    alert(b);
}(function () {
    return 5;
}());

// I missed a semicolon, the immediately-called-lambda is now used as an argument list

And these are just minor point. Just remember: always be consistent. Omitting the semicolon adds a minor performance penalty as well:
the “placing” of semicolons is part of error recovery. It shouldn’t be neccessary, but it is.

Sometimes the language isn’t that great, but just ignore these little handy-dandy things. You’ll be better off, and a better programmer. :-)

Tagged with:
 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>