Results 1 to 6 of 6

Thread: Syntax Question

  1. #1
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Syntax Question

    Despite having semicolons optional in JS, I like to use them whenever appropriate anyway. That being the case, I'm not sure if the following deserve semicolons (because they end in braces):

    Code:
    var a = function() {
       return 5;
       }
    Code:
    var b = {
       b: 5,
       c: 3
       }
    Or is the answer more a coding preference?

  2. #2
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    They do, they are simple variable assigments, so if
    Code:
    var i = 0;
    needs a semicolon, then
    Code:
    var i = function() { return 0; };
    needs it as well.

    Automatic semicolon insertion is done at that place anyway.

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,025
    Thanks
    44
    Thanked 3,210 Times in 3,172 Posts
    Blog Entries
    12

    Default

    You really answered your own question, it is a preference. However, if any line that has ended deserves a semi-colon, these do. There are cases where adding a semi-colon after a closing brace will mess things up (at least I think I have seen this), but these are not examples of that.

    I generally try to get all the semicolons in when coding, and it is good form. They are only required when the meaning could be ambiguous to the script parser. They cannot be used if the end of a statement is not appropriate at that point.
    - John
    ________________________

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

  4. #4
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Alright, thanks for both your replies. I just wasn't sure because I've seen it done both ways, but yeah, the more I think of it, it does make sense to have them there. I suppose a good splash test is to see whether or not the code works if you don't use line breaks at the end of any statement.

    (Oh, just a random question, how many posts does it take to get an avatar? Thought it was 150 posts.)
    Last edited by Trinithis; 07-17-2007 at 07:06 PM.

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,025
    Thanks
    44
    Thanked 3,210 Times in 3,172 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Trinithis View Post
    Alright, thanks for both your replies. I just wasn't sure because I've seen it done both ways, but yeah, the more I think of it, it does make sense to have them there. I suppose a good splash test is to see whether or not the code works if you don't use line breaks at the end of any statement.

    (Oh, just a random question, how many posts does it take to get an avatar? Thought it was 150 posts.)
    Yeah, taking out the line breaks is the acid test.

    I think ddadmin is the only one who knows for sure what the post requirement for avatars is currently set at. I have mod privileges, but I cannot even see, let alone edit the screen where that information is stored.
    - John
    ________________________

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

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

    Default

    They are only required when the meaning could be ambiguous to the script parser.
    However, those cases are more widespread than the beginner might expect. For example:
    Code:
    var a = 1, b = a
    (a + b) * 3
    ... will break: the interpreter attempts to parse it as:
    Code:
    var a = 1, b = a(a + b) * 3;
    since this is valid.
    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!

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
  •