Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Using semi-colons or NOT!

  1. #1
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using semi-colons or NOT!

    Whats the skinny with NOT using semi-colons in javascript?

    I guess I am using to using semi-colons to "cleanly" seperate code, but I guess that just my style.

    But I see example scripts without semi-colons and in one case, where I was studying the code, I added semi-colons, and it change (didn't behave as expected) the script. That might be something I did wrong but I didn't see it.

    So whats the story? Use SEMI-COLONS or NOT?

    thanks

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Generally, in javascript code, semicolons may be added to the ends of lines and in many cases this eliminates the need for a line break in the code, saving one character (if that is important) or simply making the code clearer. However, there are many situations in which adding a semicolon to the end of a line of script will break the script. If you don't fully understand the code or don't know javascript fairly well and the code works 'as is', best to leave it alone.

    Various situations can arise where a semicolon is not called for. One that leaps to mind is a train of variable declarations:

    Code:
    var p='px', a=0,
    x='width', y='height', crossobj,
    busy=false;
    for example. Putting semicolons at the end of those lines without them will break the train.

    Breaking up for in's and if, else if, else trains inappropriately with semicolons are two more. I'm sure there are others.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for your input. I see your point about "trains." Thats probably what happen when I was breaking up the example js and it then failed to behave right.

    Probably a poor example, but how about?

    var x var y

    ??

    Without trying it myself, is this syntaxically incorrect?

    I guess I am too much old school, having learned 25+ different languages in my time, where structure and clean code writing/reading "meant alot" no doubt Java/Script is pretty "democratic" about things :-)

    Anyway, I appreciate your input.

    ---
    HLS

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    var x var y
    I think in itself that is syntactically invalid javascript. Its intent seems to be to declare x and y as variables. This can be done a number of ways:

    Code:
    var x
    var y
    Code:
    var x;
    var y;
    Code:
    var x
    var y;
    Code:
    var x; var y;
    Code:
    var x, y
    Code:
    var x, y;
    Code:
    var x,
    y;
    etc. In this situation the semicolon and the line break are interchangeable, as long as a comma delimited train is not involved.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    See ECMA-262 for the full rules of exactly where semicolons are inserted.

    I tend to seperate statements with both a semicolon and a linebreak. It's just good style: it makes the code easier to read, and I figure that if the parser is going to insert semicolons anyway, I might as well get there first; it might even have a positive effect on performance.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks, I'll read up on ECMA-262.

    Like yourself, I use well outlined, documented clean code with semi-colons and line-breaks. Generalize functionality and reusability is big with me.

    I guess its a matter of what one is use to. I will venture among the top three languages during the 80,90 and into the 2000s was BASIC, PASCAL and C/C++. BASIC does not use semi-colons. So with the JAVA/SCRIPT language becoming ever so popular, what you see, probably tells you more what language the JS programmer was more familar with in the past. So when I see semi-colon-less code, I think "must be a VB programmer"

    ---
    HLS

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    And that's enough of an insult to cause them to fill their code so full of semicolons it's hard to make out the actual text
    the JAVA/SCRIPT language
    It's actually called ECMAScript, but commonly referred to as Javascript, which was its predecessor. I don't know if you're confusing it with Java there as the slash implies, and pardon me if you're not, but Java and ECMAScript are two totally different languages; they just happen to have similar names. Java is much more like C++ than ECMAScript.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oh, I do not mean to start a language debate. :-) Currently, I happen to work in a mixed language product development environment with Pascal, C/C++, VB, Java for our native language SDK/API and a higher layer/wrapper with our own p-code (Wildcat!) BASIC application development language. I pretty much stayed away from JavaScripting for many years simply for various reason probably mostly because we didn't need it and for maximum browser compatibility. Currently, the Java version SDK is for our persistent web-based client interfaces. So I would be the last person on earth to enter into a language debate. :-)

    But we are now fancying up the interface and WEB SDK/API with AJAX, thus working more with JavaScripting (and DOM) now. I wasn't sure if there was a technical reason or "programming preferrence" for not using semi-colons in JavaScripts.

    It sounds like it just a matter of style? Nes pas?

    ---
    HLS

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Oh, I do not mean to start a language debate. :-)
    No, I was just taking a passing snipe at Visual Basic and all its modern ilk.
    It sounds like it just a matter of style? Nes pas?
    Mostly so, yes. As I said, though, the parser inserts semicolons in appropriate places before execution. Adding semicolons reduces ambiguity.

    Astonishingly, I actually agree with something posted on MSDN.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I like to use semicolons wherever they belong, if I'm not in a hurry. If I am in a hurry, I tend to go back later and put them in. It just makes things clearer to me, what I intend the code to do. This does occasionally result in code I write missing a few semicolons, as long as the parser can handle it.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •