19

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.

1 Answer 1

38

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.