Results 1 to 5 of 5

Thread: JavaScript condition question

  1. #1
    Join Date
    Jul 2008
    Posts
    34
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default JavaScript condition question

    I am just a novice in javascript and programming, but I like programming a lot. I did not take any classes about programming, and all the basics I have for javascript come from google and... here at the forum. While it is not always the case, but most of the scripts here were written in such a, what to say, dense format (in my opinion) that it is really difficult, especially newbie like me, to follow and understand them. In order to understand any script, I usually have to reformat them in an easier format to read, for example:
    Code:
    statement1;
    statement2;
    if (conditions) {
      statement3;
    }
    ie adding extra semicolon ";", brackets "{,}", tabs and spaces (I prefer tabs) and make all the lines as clear as possible so that I can follow them.

    While I not only totally respect all people here with all their works and scripts but also learn a lot from reading their codes and from the forum, I wonder:

    a) when and why we need semicolon ";", comma "," or even none? what should I normally put at the end of lines when coding? When I reformat the script, I always put (or replace if needed) semicolon in the end of each statement. Am I doing right or not?

    b) is this the typical style of javascript scripts when people write them? when I read some other scripts (php, java, c etc...) I usually see the nicer format than formats of the scripts here.

    Thanks,

    D.
    Last edited by ddadmin; 07-17-2008 at 08:56 PM.

  2. #2
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,876
    Thanks
    49
    Thanked 264 Times in 256 Posts
    Blog Entries
    56

    Default

    My guess is that you are primarily referring to javascript-conventions. If that's the case, here's a good (basic) tutorial.
    ===
    Arie Molendijk

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

    Default

    No, that is not a good tutorial (few of W3Schools' works are, I fear; people have been complaining at them in their forums for years, but they pointedly ignore all criticisms of their terrible and often entirely misleading tutorials). http://www.howtocreate.co.uk/tutoria...ipt/semicolons addresses one of your issues. For the other, I can offer no better explanation than that Javascript is a language commonly not taken seriously, so people writing it do not invest a proper amount of time into making it readable. This is a big mistake, especially with complex scripts -- Javascript's functional nature and the deeply-nested scopes that result from it in good code can become very hard to read, making proper indentation and source-code formatting even more important than in less-powerful, imperative languages such as Java, C, or PHP, the latter of which also suffers from this problem in a lot of newbie code.

    Braces, though, it must be said, can often detract from the readability of code if over-used. I believe that they should be used as sparingly as possible, although opinions and site code regulations may differ.

    The scripts in DD's archives, while intermittently inspired, should not by any means be taken as a measure of good code quality. I had a go at reforming them, but it is rather a daunting task, and I fear other obligations took priority. Please feel free to take up the torch — any improvements are welcome.
    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!

  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

    I agree with what Twey wrote (nice to see you again). I would add that, just as an example using the one you offered:

    Code:
    statement1;
    statement2;
    if (conditions) {
      statement3;
    }
    That could be written:

    Code:
    statement1;
    statement2;
    if (conditions)
      statement3;
    And then one might be tempted to do this:

    Code:
    statement1;
    statement2;
    if (conditions);
      statement3;
    But it will cause an error, at least in browsers that don't error correct it for you while parsing it.

    The semicolon is for the end of a line of code (not necessarily a line of text in the code), usually a statement, so this would be fine:

    Code:
    statement1;
    statement2;
    if (conditions) {
      statement3;
    };
    The comma is for separating common things like a bunch of arguments, a line of variable declarations, or a line of property assignments, items in an array. When I say line here, again I mean a code line, not necessarily a line of text in the code.
    - John
    ________________________

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

  5. #5
    Join Date
    Jul 2008
    Posts
    34
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    No, that is not a good tutorial (few of W3Schools' works are, I fear; people have been complaining at them in their forums for years, but they pointedly ignore all criticisms of their terrible and often entirely misleading tutorials). http://www.howtocreate.co.uk/tutoria...ipt/semicolons addresses one of your issues. For the other, I can offer no better explanation than that Javascript is a language commonly not taken seriously, so people writing it do not invest a proper amount of time into making it readable. This is a big mistake, especially with complex scripts -- Javascript's functional nature and the deeply-nested scopes that result from it in good code can become very hard to read, making proper indentation and source-code formatting even more important than in less-powerful, imperative languages such as Java, C, or PHP, the latter of which also suffers from this problem in a lot of newbie code.
    Thanks for the link, Twey. As advised in the article, it is useful to always use semicolon, even if not needed, and that is what I have been doing. Just wonder why javascript is so different than, for example, php. PHP accepts semicolon only (as far as I know of).
    Quote Originally Posted by Twey View Post
    Braces, though, it must be said, can often detract from the readability of code if over-used. I believe that they should be used as sparingly as possible, although opinions and site code regulations may differ.
    Not in the case of loop inside another loop, at least for me. I always have difficulties to follow such situation, to understand what statement belongs to which loop.
    Quote Originally Posted by Twey View Post
    The scripts in DD's archives, while intermittently inspired, should not by any means be taken as a measure of good code quality. I had a go at reforming them, but it is rather a daunting task, and I fear other obligations took priority. Please feel free to take up the torch — any improvements are welcome.
    I am at the level of learning and understanding codes, and then trying to modify, corporate different codes together for my own purpose. I dont think I am good and knowledgeable enough to make any improvements, especially the browsers' compatibilities that the codes should have to be cross-browser (meaning to be applicable and widely used). If, in the future, I feel that I am capable, then I will not hesitate to take the torch

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
  •