News Flash: Semicolons Required in JavaScript

|

These days, arguing over whether semicolons are optional in JavaScript seems to be all the rage.

Many people say that semicolons are optional in JavaScript. But JavaScript will insert them for you if they don’t exist. So clearly, JavaScript thinks they’re necessary, otherwise it wouldn’t insert them for you. Besides, if semicolons were optional, it wouldn’t be called “Automatic Semicolon Insertion,” but instead something like “Automatic Handling of Semicolon-less Code.”

Semicolons are required in JavaScript; they’re just not required in YOUR JavaScript.

Quod erat demonstrandum.

Learn more about ASI

Read JavaScript Semicolon Insertion: Everything you need to know and An Open Letter to JavaScript Leaders Regarding Semicolons. They’re great articles, and contain a wealth of useful information.

If you prefer ultra-technical documentation, read the Automatic Semicolon Insertion section of the ES5 spec. It isn’t a particularly fun read, but it gets down to the nitty gritty.

And once you’ve learned all you care to know about ASI, make your own informed decision.

My preferences

I personally find having to remember all the ASI rules tiresome, so I use semicolons freely and lint all my JavaScript with JSHint via grunt, which tells me if there are extras or if some are missing.

grunt failing lint output

An overabundance of semicolons and curly braces isn’t exactly what I’d consider aesthetically pleasing, but I’ve found that being a little more explicit makes my code more approachable for beginning JavaScript programmers.

Considering the number of confusing parts in JavaScript including (but not limited to) global scope, ASI, hoisting, with, this, == vs ===, new, typeof and prototypal inheritance, I’ve found that beginning JavaScript programmers need all the help they can get.

Post A Comment

  • Any of these HTML tags may be used for style: a, b, i, br, p, strong, em, pre, code.
  • Multi-line JavaScript code should be wrapped in <pre class="brush:js"></pre>
    (supported syntax highlighting brushes: js, css, php, plain, bash, ruby, html, xml)
  • Use &lt; instead of < and &gt; instead of > in the examples themselves.