r/javascript • • Apr 01 '18

Google publishes a JavaScript style guide. Here are some key lessons.

https://medium.freecodecamp.org/google-publishes-a-javascript-style-guide-here-are-some-key-lessons-1810b8ad050b
714 Upvotes

317 comments sorted by

View all comments

293

u/kevinkace Apr 01 '18

Semicolons ARE required

👍

29

u/kobbled Apr 01 '18 edited Apr 01 '18

thank god. JS semicolon inference leads to so many bugs.

43

u/mmcnl Apr 01 '18

I've never encountered a bug due to semicolon inference.

15

u/hyperhopper Apr 01 '18
var a = 5
var b = 10
[a, b] = [b, a]
console.log(a, b)

Then add semicolons to make it work like it should

3

u/Voidsheep Apr 02 '18

Pretending ASI doesn't exist is also dangerous, it's not like you can disable it.

function shouldHitTheBrakes() {
  return
    isSmeoneOnTheWay ||
    isTurnAhead ||
    isAboveSpeedLimit
}

2

u/hyperhopper Apr 02 '18

Extremely true!

Though I've never seen somebody use a return without something on that line. Plus any linter will yell at you for unreachable code in that case.

7

u/[deleted] Apr 01 '18

[removed] — view removed comment

3

u/hyperhopper Apr 02 '18

Don't be pedantic. Destructuring with arrays makes this type of bug easy to run into, and this example shows that.

11

u/allenthar Apr 01 '18

Okay.

var a = 5 var b = 10 ;[a, b] = [b, a] console.log(a, b)

I have literally never had this situation come up while coding JS, and I’ve been doing it full time for a few years now.

Regardless, Prettier handles automatically removing or adding semicolons to my code. So it’s literally just a linter setting at this point.

→ More replies (5)

0

u/[deleted] Apr 01 '18

[deleted]

10

u/Mingli91 Apr 01 '18

Yeah, been using Standard JS since my last job and I love it. Not had a single bug from omitting semicolons and now I have a far better understanding of ASI in JavaScript

5

u/JohnMcPineapple Apr 01 '18 edited Oct 08 '24

...

3

u/nayocum Apr 01 '18

If you use an IIFE it can cause issues if your last line didn't have a semicolon as well

→ More replies (4)

5

u/aaarrrggh Apr 02 '18

Is this a joke?

I’ve not been using semi colons for over a year on personal projects.

Number of bugs caused by this decision: 0.