I have seen and used javascript code that both has and omits the ‘;’ from the end of statements. Are they required in some cases and not in others? If so can you give some examples, and if not what is the general standard, to use the ‘;’ or to not…that..is the question??
Share
JavaScript uses a (generally disliked) method called Semicolon Insertion, where it will allow you to omit semicolons.
The rules for where you can and cannot insert semicolons is extensive, and covered fairly well here.
In general, do not rely on semicolon insertion. It is a poorly thought out feature and will hurt you before it helps you.
Not only should you always use semicolons, but you should also get in the habit of putting your
{braces on the same line, as semicolon insertion can actually misinterpret what you meant.For example, say I’m trying to return a javascript object from a function:
If I put the
{on a new line (as I did ing), the return statement will actually have a semicolon inserted after it, destroying the meaning of what you were saying.Overall, semicolon insertion is a poor feature and you should never rely on it.