Why semicolons?

Maybe another fact here is, that some big tech companies do forbid dropping semicolons in languages that do not require them. Examples are the AirBnB and Google JavaScript code styles. They felt, that allowing people not to use semicolons was actually not worth the trouble and therefore require semicolons in their code, even if they are not strictly necessary.

From personal experience, I think semicolons make reading code just way easier since you once an expression is finished. Especially when looking at multiline statements.

2 Likes

That's different. It's not that JS doesn't require semicolons; it's that it automatically inserts them, sometimes changing the meaning of code in the process.

That terrible design decision built into the language makes automatic semicolon insertion everyone's problem. If you come into JavaScript from languages with semicolons and start writing code with semicolons like you're used to writing, you'll no doubt eventually get bitten by this:

// This doesn't do what you'd expect
function foo(y) {
    let x = 1;
    return
        (x + 1) * y;
}

The only sensible way to write JS therefore is to memorize the rules for automatic semicolon insertion, at which point explicit semicolons at the end of lines are nothing more than garnish.


Edit:

I misread due to the double negative. They require semicolons? I find that surprising.

Edit 2:

I see. They recommend semicolons so that a linter can catch code that is broken by automatic insertion. That's... brilliant, actually.

2 Likes