How are semicolons in javascript important?

331    Asked by OkamotoBando in Java , Asked on Oct 12, 2022

 It seems to be fashionable recently to omit semicolons from Javascript. There was a blog post a few years ago emphasizing that in Javascript, semicolons are optional and the gist of the post seemed to be that you shouldn't bother with them because they're unnecessary. The post, widely cited, doesn't give any compelling reasons not to use them, just that leaving them out has few side-effects.

Even GitHub has jumped on the no-semicolon bandwagon, requiring their omission in any internally-developed code, and a recent commit to the zepto.js project by its maintainer has removed all semicolons from the codebase. His chief justifications were:

it's a matter of preference for his team;

less typing

Are there other good reasons to leave them out?

Frankly I can see no reason to omit them, and certainly no reason to go back over code to erase them. It also goes against (years of) recommended practice, which I don't really buy the "cargo cult" argument for. So, why all the recent semicolon-hate? Is there a shortage looming? Or is this just the latest Javascript fad?


Semicolons in javascript makes method chaining easier and commit diffs cleaner

So let's say I'm jQuerying about and I have

$('some fancy selector')
  .addClass()
  .attr();
If I want to add stuff and keep my line-based commit diff small, I have to add it above attr. So it's one thought longer than just "add at the end". And who wants to think? =)
$('some fancy selector')
  .addClass()
  // new method calls must go here
  .attr();
But, when I drop the semi-colons, I can just append and call it a day
  $('some fancy selector')
    .addClass()
    .attr()
+ .animate()
+ .click()

Also, if I decide to pop off the last method, I don't have to reassign the semi-colon and pollute my commit again.

  $('some fancy selector')

    .addClass()

    .attr()

    .animate()

- .click()

Versus the uggo

  $('some fancy selector')

    .addClass()

    .attr()

+ .animate();

- .animate()

- .click();



Your Answer

Interviews

Parent Categories