I've only written a small amount of JavaScript that runs embedded in a Java application, but it was tested using QUnit, has been mixed, and I've not noticed any problems yet.
Is there some conventional wisdom whether to use semicolons or not in JavaScript?
Now that we know that JavaScript automatically adds semicolons to certain statements with a few restrictions and rules, we can—as a best practice—use semicolons after finishing statements such as variable declaration with var, let, or const, when calling a function, using ++ or –, and when using return, break or ...
This is all possible because JavaScript does not strictly require semicolons. When there is a place where a semicolon is needed, it adds it behind the scenes. This is called Automatic Semicolon Insertion.
It won't harm to put a semicolon after the { } of an if statement (it will be ignored, and you might see a warning that it's unnecessary).
Use them. Use them constantly.
It's far too easy to have something break later on because you neglected a semi-colon and it lost the whitespace which saved it before in a compression/generation/eval parse.
I'd say use them all the time; most code you'll encounter uses them, and consistency is your friend.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With