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)