Wednesday, 16 September 2015

Automatic Semicolon Insertion

I've been recommending to people that they not rely on Automatic Semicolon Insertion for years now, and frankly wish that the strict variant had removed it. Just wanted to share the best argument for putting semicolons where they belong that I've seen in a long time:

var foo = {
    a: "ay",
    b: "bee"
}

(function() {
    console.log("Hi there");
})();

That blows up, because the opening ( in the inline-invoked function expression at the end is interpreted as the opening parentheses of a function call, with the {...} being what it tries to call. Since that's not a function, it fails with TypeError: (intermediate value)(intermediate value) is not a function.

As the lady said: Ain't nobody got time for that! Use your semicolons.

1 comment:

William said...

Very good point.