Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    60
    Thanks
    65
    Thanked 0 Times in 0 Posts

    javascript standards

    Hi Could you please point me in the right direction when it comes to javascript standards. I have search online and i see that there is no correct way to go-- is this right?



    1) is it better to use single or double quotes
    var test = 'string';
    var test = "string";

    $("#test").val();
    $('#test').val();

    2)do function names start with a upper or a lower case?


    thanks
    Last edited by code-in-time; 03-26-2012 at 08:17 PM.

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    208
    Thanks
    52
    Thanked 1 Time in 1 Post
    Double or single quotes depend on whether or not you want a multiline
    string
    Code:
    var string1 = "this is a \n multiline string";
    var string2 = 'this is not a \n multiline string';
    // in some cases in Internet Explorer;
    // you will have to use \n\r for new lines
    also, if you have the same type of quote inside a quote set:
    'here is a string \'with the same type of quote\' inside'
    or
    'here is a string "with the same type of quote" inside'
    or
    "here is a string 'with the same type of quote' inside"
    further, in English at least 'can't' will not work. Use "can't" or 'can\'t'

    for built in functions and methods you will have to refer to a text like O'Reilly's
    Javascript: The Definitive Guide. I believe the latest revision is the
    5th edition.
    IF you write your own functions, it does not matter. Just call them or
    refer to them with the same name (including caps and lower case)
    that you gave them

  • Users who have thanked anotherJEK for this post:

    code-in-time (03-26-2012)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by code-in-time View Post
    1) is it better to use single or double quotes
    var test = 'string';
    var test = "string";

    $("#test").val();
    $('#test').val();
    Yes. Definitely. If you use anything else your code won't work. <grin/>

    Okay, sorry. Couldn't help myself. It DOES NOT MATTER. Personally, I always use "..." unless the string needs to contain a ", in which case I then use '...'. I know I can use \" to put the " inside of "...." but that looks ugly to me and I only use it as a last resort.

    2)do function names start with a upper or a lower case?
    Yes. Or an underline. Or a dollar sign. Your choice.

    Now... *MOST* people start function names with a lower case letter. But is there a rule about that? No.

    Unless you happen to work for some company that has coding standards, of course.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    code-in-time (03-26-2012)

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,465
    Thanks
    0
    Thanked 634 Times in 624 Posts
    The built in functions/objects use an uppercase letter to start those that are intended to be used as the basis for creating your own objects (where they'd be classes in an object oriented language) while the built in functions and objects intended to be used as supplied start with a lowercase letter.

    This convention is often followed by JavaScript programmers for the functions and objects they create themselves but this is just a convention and there is nothing to force you to do this (unless your complany standards state otherwise).

    In the same way there is a convention (but not a standard) to use an underscore on the front of private functions as a reminder that they have limited scope.

    The actual JavaScript standards can be found at http://www.ecma-international.org/pu...s/Ecma-262.htm (for the core portion of JavaScript) and http://www.w3.org/DOM/ (for the document object model part).

    The JavaScript validator can be found at http://jslint.com - for JavaScript attached into web pages the only checkbox at the bottom that you need to check is "Assume a browser" so that it doesn't give errors relating to the window object (which only exists when JavaScript is run in a web browser).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    code-in-time (03-26-2012)

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Quote Originally Posted by anotherJEK View Post
    further, in English at least 'can't' will not work. Use "can't" or 'can\'t'
    or 'can’t' (which would be typographically correct)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    code-in-time (03-26-2012)


  •  

    Posting Permissions

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