2

In the book "Beginning JavaScript" by Jeremy McPeak and Paul Wilton there's a do-while loop example.

var userAge;
do {
   userAge = prompt("Please enter your age","")
} while (isNaN(userAge) == true);

and below the author repeats one more time:

userAge = prompt ("Please enter your age","")

There's no semicolon at the end of the statement inside the do {} block. As far as I can remember the author stated that it's considered best to always end any statement with a semicolon although it's not a must in most cases.

Was the omission of ; intentional? It seems so considering that in both the 4th and 5th editions there's no semicolon. And there's also a repetition of that line below which doesn't contain a semicolon either. Of course, one might say that it's not even a mistake. What I want to understand is whether the omission of a semicolon is more likely to be a typo or (which is worse) rather was done intentionally. If the latter is true, isn't that a sign of inconsistency?

Don Draper
  • 463
  • 7
  • 21
  • Semicolons are optional in JS. See https://www.npmjs.com/package/standard for example. – Félix Saparelli Jan 07 '16 at 12:25
  • 3
    For the *why*, it would be better to ask the author. – alex Jan 07 '16 at 12:28
  • Appreciate your question but never follow any book 100%. – Harsh Sanghani Jan 07 '16 at 12:28
  • I know about "Automatic Semicolon Insertion". But the author recommends using them elsewhere. – Don Draper Jan 07 '16 at 12:28
  • @HarshSanghani, I do realise that none of the books is error-free. But it's so disappointing to see a type or inconsistency in the book that you learn from. Now it's hard for me to trust the rest of the book. – Don Draper Jan 07 '16 at 12:31
  • better to ask author but in this case it is single statement inside do while so not necessary to put semicolon – Shree29 Jan 07 '16 at 12:32
  • You'd have to ask the author. We don't know what he was thinking at that moment. –  Jan 07 '16 at 12:32
  • There doesn't need to be, but there should be. – Madara's Ghost Jan 07 '16 at 12:33
  • 2
    @MadaraUchiha: As to your example of an ASI issue you [posted](https://gist.github.com/MadaraUchiha/04bae5b1f2dbb78fd8e1), that comes from the existence of ASI, not from people taking advantage of it. There's no place in the code example you posted where one could manually insert a semicolon to fix it. –  Jan 07 '16 at 12:40
  • 1
    For me its absolut nonsense to type and delete and skip the `;` all the time. I only have problems when using them. The few mistakes you can make, you make them at max once and then remember – CoderPi Jan 07 '16 at 12:43
  • Another bad practice is that the author is using "==" instead of "===". Most Javascript specialists that I have read recommend using "===" in all cases. – Don Draper Jan 07 '16 at 12:44
  • @CodeiSir What about when you get bugs introduced because of two different files being included together through a build process? Independently they're fine, but together they have a bug. ASI is/was a foolish move, there's a good reason they're required in most (all?) other C-like languages that had more than a couple of days thought put in to them. JS was a rushed design initially, and now we're stuck with it. – James Thorpe Jan 07 '16 at 12:46
  • 1
    @TimurSharapov: The rules of `===` are certainly simpler but that's another case where sometime you do want the type coercion provided by `==`. I usually use `===` but certainly not always. –  Jan 07 '16 at 12:46
  • @JamesThorpe Nowadays I almost only use TypeScript, so screw that problem ;) – CoderPi Jan 07 '16 at 12:51
  • 1
    @JamesThorpe: I can't speak for CodeiSir but I know that people who elide semicolons usually have a set of basic practices where they do include them. They're usually *less* likely to have bugs because the handful of places where they are needed stand out, whereas in the myriad of manual insertions, it's easy to miss one and cause a bug. –  Jan 07 '16 at 12:52
  • Nicholas Zakas, Dr. Axel Rauschmayer write in their books that always ending every one of your statements with a semicolon is recommended and helps prevent errors and even imporve performance in some situations. – Don Draper Jan 07 '16 at 12:55
  • 1
    @TimurSharapov: They actually say it improves performance? That's hard to believe since the semicolons will have no bearing on the final program. Unless they meant the performance of the parser, which may be the case. Either way, they can recommend it. Nothing wrong with that. I don't use them, and my code is just fine and much cleaner without them. –  Jan 07 '16 at 12:58
  • 1
    If you were to find all the questions on StackOverflow where there was a bug due to a missing `;`, I'd bet that the *vast* majority of them do *include* semicolons in the rest of their code, and just missed one in that situation. So taking advantage of ASI usually is not the issue. See [this article](http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding) to get a different perspective. –  Jan 07 '16 at 13:03

1 Answers1

0

When writing or editing javascript, the semicolon helps a lot visually. Depending on your editor, it may not be clear otherwise if you actually have a line break in there (ending the line of code). Instead, your editor may just be wrapping the line visually. So without the semicolon, you can make and overlook errors more easily. For more info:https://www.webmasterworld.com/forum91/521.htm

Tanmay
  • 590
  • 8
  • 20