FAT

I don't write "JavaScript"

October 31st 2011

I’ve been getting asked and even hassled on the absence of semicolons in bootstraps source quite a bit lately. This sorta surprised me. So much in fact i had to create an entire blog just to write about it…

Despite what you may have heard, you have options when you write JS. LOTS of options. It’s pretty neat. Consider this simple bit of code:

if (event.preventDefault) {
  event.preventDefault()
}

Pretty straight forward. However, the brackets are optional and the above code could be rewritten to look something like this:

if (event.preventDefault) event.preventDefault()

If you were really having fun with it you could lose the if all together and do something like this:

event.preventDefault && event.preventDefault()

Each is perfectly valid. Each behaves the same. It’s just a matter of preference and finding the style that makes most sense to you.

With a syntax as relaxed as javascript’s, you see lots of different approaches to the same problem. Unfortunately, this unbound diversity often results in at least one particularly alarming approach (this approach will likely have been written by Jason Stirman <3 ). Unfortunately, encountering something like this can drive people towards an unnecessarily strict linter like JSLint.

For many frontend engineers, javascript is still only “javascript the good parts”. It’s still JSLint’s “JavaScript”.

But it doesn’t need to be. Again, you have options. (If you haven’t already I highly recommend you stop what you’re doing and checkout JSHint).

Ok, ok… Why are there no semicolons in bootstrap?

So to directly answer the original question…. Technically there are semicolons in bootstrap.

The majority of lines however don’t end with semicolons because they simply aren’t necessary and I prefer the minimalist aesthetic. For me, \n character is enough and the semicolon character is redundant. Here’s an excellent article by @izs which goes into greater detail on the issue.

That said, there is one technically optional location that I’ve added semicolons to in the bootstrap source: the very end of each file. These were added for developers who were curling bootstrap plugins and concatenating them with rails generated JS.

Although that seems innocent enough, there exists this highly unfortunate pattern:

(function () {
  // le code
}())

Because ()