2

So his help site states that "All statements, including function declarations, must be correctly terminated with semi-colons."

But in this sample code, he specifically say not to end the if statement in semicolon.

So is there a complete list of what should be ended with a semicolon? I was looking at my Javscript code and here are some cases I wasn't sure was proper format for the packer:

1)

for( i in cities ) {
    alert( i );
};

2)

var map = {
    city : 'atlanta',
    year : 1987
};

3)

var info_window = new google.maps.InfoWindow( {
    content : content_div,
    zIndex  : INFO_WINDOW_Z
}; );

4)

var options = {
    business : business,
    columns  : [ 'url', 'image_url', 'expiration', 'percent_discount', 'claimed', 'fine_print' ];
};

5)

$( warp_content ).hover( function() {
    $( deal_description ).fadeIn( 'fast' );
};, function() {
    $( deal_description ).fadeOut( 'fast' );
}; );
hobbes3
  • 28,078
  • 24
  • 87
  • 116
  • Note how that page says "this page needs to be updated for version 3.0". I tried some code without semicolons and it all appears to work just fine; I assume the semicolon warning is outdated. – icktoofay Feb 10 '12 at 04:21

1 Answers1

2

There are rules, but sometimes they may not be obvious. Essentially, the following statements do not need terminating semicolons:

  • if (...) { }
  • for (...) { }
  • while (...) { } (except in do { } while (...);)
  • function (...) { } (except for examples such as var f = function() { }; where an anonymous function is part of a larger statement)
  • try { } catch (...) { }
  • with (...) { }

Essentially, anywhere { } surrounds a group of statements, that is a block and no terminating semicolon is required.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • This is a good answer, but to clear up one potential ambiguity: in `var x = function () { ... }` you need the semicolon, because even though the `{ ... }` surrounds a group of statements, it appears inside a statement that does need a semicolon. If the next statement was `(function () { ... })();` then leaving the `;` off the end of the `var` would cause the meaning of the program to change. – Mike Samuel Feb 10 '12 at 04:17
  • Well the packer said that function declarations need to end with semicolons like this `function() {};`, what about objects like 'var map = { this : 'that' };`? – hobbes3 Feb 10 '12 at 04:18
  • @MikeSamuel: Thanks, I clarified my answer. – Greg Hewgill Feb 10 '12 at 04:20
  • 1
    @hobbes3, `function () {};` can be a function value when used inside an expression, but it cannot be a function *declaration* since it has no name. Function *declarations* need not end with a semicolon. – Mike Samuel Feb 10 '12 at 04:20
  • @hobbes3: Objects like `{ this : 'that' }` use curly braces as part of the *object literal syntax*, not as a *block of statements*. – Greg Hewgill Feb 10 '12 at 04:21
  • 1
    @hobbes3, Yes, you need a semicolon after `var map = { "this": "that" }`. Try running `var map = { "this": "that" } /* newline here */ (function () { }());` and see what happens without the semicolon. – Mike Samuel Feb 10 '12 at 04:22