Semicolon Insertion

Many languages like C, Java, and JavaScript end statements with semicolons (";"). JavaScript introduces a syntactic convenience so that semicolons can be omitted in some places.

foo()  // No semicolon needed.
bar()

This is often useful, but unfortunately the rules around where semicolons can be inserted are tricky. Consider the following code snippets that change meaning radically based on whether a semicolon is present or not:

var alertWrapper = function (msg) { alert(msg) };

(function () {
  // do stuff
})();

// Could be
// 1. a function definition and a block of initialization code.
// 2. a declaration initialized to the result of chained function calls.
foo.bar();

[doThis, doThat, doTheOther][functionIndex]();

// Could be
// 1. a not uncommon idiom for function dispatch.
// 2. a property lookup on the result of foo.bar()
foo.bar();

/myRegex/g.test(baz) ? doThis() : doThat();

// Could be
// 1. a short form of if-else based on a regex
// 2. an if-else based on the result of a division.
function fib(i) {
  if (i > 2)
    return;  // Compute recursively
        fib(i - 2) + fib(i - 1);
  return 1;  // The base case
}

To help you get into the habit of not leaving out semicolons, except before closing braces ("}") and the end of a compilation unit, the linter will issue warnings where semicolons are inserted.

The linter can also help you diagnose problems with semicolons not being inserted where you expect them to be.

As always, to turn this off, put the error message name SEMICOLON_INSERTED in the