0

In most cases, we can omit semi colon at the end of a statement. But there is a exception I can tell.

isGreenBottleFull && clearGreenBottle()
isRedBottleFull && clearRedBottle()

If you omit the semi colons, the behavior will be strange.

My question is that is there any other cases in which the semi colon is required?

zzzgoo
  • 1,536
  • 2
  • 9
  • 23
  • It is generally good practice to include the semicolon. It will make life so much easier when you come to minify your code! – BenM May 10 '18 at 17:47
  • Do **not** omit semicolons. Just don't do it. – ASDFGerte May 10 '18 at 17:48
  • 3
    Is this really an example that demonstrates a difference based on a semicolon? These lines are not readable as a single statement when removing the line break, so I don't see how it could produce different results with or without a semicolon at the end of the first line. – apsillers May 10 '18 at 17:48
  • This code doesn't work either way as is. None of the variables/functions are defined. – cHao May 10 '18 at 17:54
  • @apsillers you're right - those examples will not lead to "strange behaviour". In fact, I keep being surprised at people's thoughts on semicolons in JS. Yes, omitting them *can* lead to some odd behaviours but rarely, if ever, do people who criticise them offer valid examples. It's as if they believe JS starts running on black magic if there is no semicolon. – VLAZ May 10 '18 at 18:00
  • @vlaz It's hard to come up with examples on demand, but I've seen several questions in SO where the reason for the problem was unexpected ASI behavior. – Barmar May 10 '18 at 18:31
  • If you use semicolons all the time, you can never be surprised by what ASI does. – Barmar May 10 '18 at 18:31
  • @vlaz Here's an example: https://stackoverflow.com/questions/3641519/why-does-a-results-vary-based-on-curly-brace-placement/3641525#3641525 – Barmar May 10 '18 at 18:33
  • [another example](https://stackoverflow.com/questions/42036349/uncaught-typeerror-intermediate-value-is-not-a-function/42036382#42036382) – Barmar May 10 '18 at 18:34
  • [more examples](https://stackoverflow.com/questions/22280267/what-sort-of-strange-things-can-happen-when-omitting-semicolons-using-javascri/22280311#22280311) – Barmar May 10 '18 at 18:36
  • @Barmar the newline after `return` is another one that bothers me. Yes, it's ASI that will trip you up but it's *not* because you forgot the semicolon. No matter how many semicolons you use, the `return` still won't work multiline. The IIFEs after non-semicolon terminated line are much better as an example. But, again, it's very rare when people use them or other legitimate examples of ASI making code behave differently. E.g. `var a = 2\n[1, 2, 3].forEach(x => console.log(x * a))` - because the line starts with `[` it will be considered part of the previous line which is not the intention. – VLAZ May 10 '18 at 19:05
  • @vlaz You're right, the most pernicious cases are when ASI adds a semicolon that you didn't want, rather than it leaving out a semicolon that you expected when you ended the line. – Barmar May 10 '18 at 19:09

1 Answers1

0

Semicolons in JavaScript are rarely required for code to execute properly, however are considered a best practice for code quality.

I would recommend this reference as a guide: https://www.codecademy.com/en/forum_questions/507f6dd09266b70200000d7e

MARyan87
  • 1,121
  • 1
  • 8
  • 15