Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating multiple statements using Commas vs Semi colons

While working with minified Jquery I noticed multiple statements concatenated by commas.

For Example

event.preventDefault();
event.stopPropagation();
jQuery.event.trigger( event, data, this[0] );
return event.result;

minified to:

c.preventDefault(),c.stopPropagation(),d.event.trigger(c,b,this[0]);return c.result

I did see a similar question : JavaScript variable definition: Commas vs. Semicolons

But in this case, obviously there is no performance benefit or reduction of characters.

Could I assume it was just a matter of choice for the minification script to do so?

Wouldn't it have been more safe to go with semicolon in the first place.. and prevent the overhead of the logic which avoided concatenation of commas in case of the return statement.

like image 553
loxxy Avatar asked Apr 23 '13 07:04

loxxy


1 Answers

Minifiers tend to favour commas where possible because it allows things like if statement bodies to be shortened. For example:

if (x) {
    y = 10;
    z = 20;
}

The above can be minified using a comma to remove the curly braces (since they are optional if the if statement body is only a single statement):

if (x) y = 10, z = 20;

In cases like this it does result in shorter code, which is what minifiers are aiming for.

like image 96
James Allardice Avatar answered Sep 28 '22 07:09

James Allardice