Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Last active March 28, 2019 08:42
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ryanflorence/61935031ff729f072d9b to your computer and use it in GitHub Desktop.
Save ryanflorence/61935031ff729f072d9b to your computer and use it in GitHub Desktop.

When a beginner asks you "when do I use semi-colons?" would you rather say this?

// what people who say "use semicolons!!" say
class Foo {
  prop = {
  }; // yes

  static bar () {
    if (stuff) {
      doStuff(); // yes
    } // no

    for (var key in obj) {
    } // no

    return {
    }; // yes
  } // no

  foo () {
  } // no
} // no

Foo.prop = {
}; // yes

things.map(thing => thing.name /*no*/)

var boing = function () {
}; // yes

function boing () {
} // no

/*no*/[1,2,3].map(); // yes
/*no*/(function () {})(); // yes

Or this?

// what people who say "don't use semicolons!" say
/*yes*/;[1,2,3].map()
/*yes*/;(function () {})()
// which are two things I haven't done in the last 3 years, mind you

In the end, I'd rather not talk about semi-colons, and just do what people around me want me to do, but when teaching a beginner who asks (and they ALWAYS ask this) "when do I use a semi-colon", I just put my face in my hands and say "I don't have any answers for you".

@nmn
Copy link

nmn commented May 9, 2015

I would add one extra rule: Never write a return statement without a value following it. So ban this:

return

and always write one of.

return undefined
return null
return 0
return someValue

This is an improvement over the always write semi-colons approach.

When you always write semi-colons, it makes you think this would work correctly:

return
  {a: 1}

However, if you don't write unnecessary semi-colons, you're already used to treating lines as statements, and are much less likely to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment