Are semicolons needed after an object literal assignment in JavaScript?

Not technically, JavaScript has semicolons as optional in many situations.

But, as a general rule, use them at the end of any statement. Why? Because if you ever want to compress the script, it will save you from countless hours of frustration.

Automatic semicolon insertion is performed by the interpreter, so you can leave them out if you so choose. In the comments, someone claimed that

Semicolons are not optional with statements like break/continue/throw

but this is incorrect. They are optional; what is really happening is that line terminators affect the automatic semicolon insertion; it is a subtle difference.

Here is the rest of the standard on semicolon insertion:

For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.

Leave a Comment