2

I am starting to learn javascriptmvc, and in code samples, I see code without semicolons at the end of expressions. Does this count on automatic semicolon insertion or am I missing something? Code example below:

$.Controller("Contacts.Controller", {
        init: function(){
            this.params = new Mxui.Data();
            $("#category .list_wrapper").mxui_data_list({
                model : Contacts.Models.Category,
                show : "//contacts/views/categoryList",
                create: "//contacts/views/categoryCreate"
            }) // <------ NO SEMICOLON

            $("#location .list_wrapper").mxui_data_list({
                model : Contacts.Models.Location,
                show : "//contacts/views/categoryList",
                create: "//contacts/views/categoryCreate"
            }) // <------ NO SEMICOLON

            $("#company .list_wrapper").mxui_data_list({
                model : Contacts.Models.Company,
                show : "//contacts/views/companyList",
                create: "//contacts/views/companyCreate"
            }) // <------ NO SEMICOLON
                            // etc...
         }
      }) // <------ NO SEMICOLON
Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
Regis Zaleman
  • 2,744
  • 5
  • 26
  • 30
  • possible duplicate of [Why use semicolon?](http://stackoverflow.com/questions/2399935/why-use-semicolon) – mplungjan Jul 10 '11 at 17:07
  • Ditto [What are the rules for Javascript's automatic semicolon insertion?](http://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion) – mplungjan Jul 10 '11 at 17:09
  • Semi-colons should be used after statements, not expressions. The addition of semi-colons to the code would explicitly make the subject lines statements. – RobG Jul 10 '11 at 23:15

3 Answers3

4

Javascript can be forgiving about the lack of a semicolon in ways that other languages are not. However a semicolon is recommended where you've pointed them out.

If you run the code you've given through JSLint, it throws up a whole stack of warnings, including complaining about those missing semicolons.

JSLint is a tool for telling you about things in your code which may not be syntax errors but which could cause problems. It generally throws up a lot of errors, even for relatively well written code, but it is good for picking up things which you should fix.

I would say that those code samples are poorly written because of the missing semi-colons.

Spudley
  • 157,081
  • 38
  • 222
  • 293
  • I would say the semicolons were pointed out are not at all mandatory. – mplungjan Jul 10 '11 at 17:45
  • @mplungjan - not mandatory, but recommended. If for no other reason than if you tried to minify that code, the lack of semicolons would cause it would break. – Spudley Jul 10 '11 at 17:52
1

Semicolons are only mandatory in inline event handlers. MOST anywhere else, a linefeed is enough. Here is an example of where it is not enough: Why is a semicolon required at end of line? And as pointed out, do not leave out semicolons if you want to minify your scripts.

Community
  • 1
  • 1
mplungjan
  • 134,906
  • 25
  • 152
  • 209
  • A linefeed is usually _not_ enough if the next line starts with a parenthesis. (e.g., `a = b + c[newline](d + e).print()` will require a semicolon somewhere on either side of `[newline]`.) – Ted Hopp Jul 10 '11 at 17:16
  • That link sort of says that, but not succinctly. Also, I wrote my comment while you were adding the link to your original post. Chalk it up to a time warp. :) – Ted Hopp Jul 10 '11 at 18:07
0

A lot of semicolons are optional in javascript, though recommended to avoid confusion

https://mislav.net/2010/05/semicolons/

Dave Powers
  • 1,510
  • 2
  • 23
  • 26
Nicklas A.
  • 6,165
  • 7
  • 38
  • 61