Skip to content
Go to file
Cannot retrieve contributors at this time
52 lines (30 sloc) 3.26 KB
title: "I don't write \"JavaScript\""
author: Fat
date: 2011/10/31
I've been getting [asked](!/nmosafi/status/129946172860268544) and even [hassled](!/pamelafox/status/128718640467021824) 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) {
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 `()` executes code in JS, when multiple functions are strung together like that, the javascript interpreter tries to execute them sequentially in a sort of chain-like fashion and throws an error. [Check it out here](//
Instead, if you are writing self executing anonymous functions, it would be safer and a bit more courteous to write them like this:
!function () {
// le code
Doing so will also save you a byte (ooo horray! i know) and you no longer need to worry about that potential syntax error. I like to call it ["smanging it"](
You can’t perform that action at this time.