A question in one of the guides for semicolon formatting


Hey, so I recently ran into this really nice guide on here about the way that you format semicolons in javascript and I had a question that more or less is based on the innerworkings on what the person writing the article had said. The guide can be found at this URL: https://www.codecademy.com/blog/78

Anyways, I really liked that article and found that I really had no issues directly comprehending what he had to say, but rather I was curious about one of the points he had made.

// NO semicolons after }:
if (...) {...} else {...}
for (...) {...}
while (...) {...}

// BUT:
do {...} while (...);

Now I think I get why the "if" one is wrong, you're basically telling the code to exit the that part of the code when you really aren't done with it. But, for the "for" and "while" statements doesn't the semicolon just ruin the effective use of the looping in each of those statements? Like, would the program would just get to the semicolon after looping once, and then say "Alright this semicolon is telling me to just exit this loop." And then it would just move on in the code?

Sorry if this was a little bit long-winded or it's a bit confusing to understand the question, but I'm curious to see if my logic is correct here. And if I am wrong, and you could provide a better understanding of it, I would be extremely grateful. I think I'm actually getting comfortable with the javascript syntax and this would help me to understand one of the things that I always had a bit of difficulty understanding. THANKS!


Well if you place the semicolon after the () of if,else if, for or while the 2. rule of the guide applies meaning that the semicolon ruins the idea of the statement. It is not that it exits the statement but, as shown in the guide, that it effectively does nothing. Also wrote a longer explanation about this issue over here:

And for semicolons after these statements e.g.

if  (...) {...} else {...};
for (...) {...};
while (...) {...};

They won't cause such a trouble as the ones after () but the statements do not require them so basically you're adding a statement that is completely pointless by using a semicolon there.


Thank you! That really helped clear up some of my misunderstandings!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.