Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are semicolons needed after an object literal assignment in JavaScript?

Tags:

javascript

The following code illustrates an object literal being assigned, but with no semicolon afterwards:

var literal = {     say: function(msg) { alert(msg); } } literal.say("hello world!"); 

This appears to be legal, and doesn't issue a warning (at least in Firefox 3). Is this completely legal, or is there a strict version of JavaScript where this is not allowed?

I'm wondering in particular for future compatibility issues... I would like to be writing "correct" JavaScript, so if technically I need to use the semicolon, I would like to be using it.

like image 850
Mike Stone Avatar asked Sep 03 '08 18:09

Mike Stone


People also ask

Are semicolons mandatory in JavaScript?

Basically, JavaScript will put semicolons in for you automatically if you leave them out. So, they aren't mandatory after all.

Should JavaScript end with semicolon?

JavaScript semicolons are optional. I personally like avoiding using semicolons in my code, but many people prefer them.


2 Answers

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.

like image 149
Jason Bunting Avatar answered Oct 05 '22 05:10

Jason Bunting


The YUI Compressor and dojo shrinksafe should work perfectly fine without semicolons since they're based on a full JavaScript parser. But Packer and JSMin won't.

The other reason to always use semi-colons at the end of statements is that occasionally you can accidentally combine two statements to create something very different. For example, if you follow the statement with the common technique to create a scope using a closure:

var literal = {     say: function(msg) { alert(msg); } } (function() {     // .... })(); 

The parser might interpret the brackets as a function call, here causing a type error, but in other circumstances it could cause a subtle bug that's tricky to trace. Another interesting mishap is if the next statement starts with a regular expression, the parser might think the first forward slash is a division symbol.

like image 24
Daniel James Avatar answered Oct 05 '22 05:10

Daniel James