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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2002
    Posts
    147
    Thanks
    31
    Thanked 2 Times in 2 Posts

    single or double quotes

    Hi

    Can you please advise on this.

    When writing javascript what is best practice.... single or double quotes?

    I tried to research this and everyone contracdicts each other?


    thanks

    A

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    There is no such thing as a "best practice" here as single and double quotes are 100% equivalent in Javascript ... if used in pairs :-)

    To avoid escaping of inner quotes you might consider using different quotes inside a string than the string delimiter quotes, but this is still up to you. Feel free to escape quotes whereever you want to :-)

    All of this is equally possible
    Code:
    var mystring1 = "This is my string";
    var mystring2 = 'This is my string';
    var mystring3 = "This is my 'string'";
    var mystring4 = 'This is my "string"';
    var mystring5 = "This is my \"string\"";
    var mystring6 = 'This is my \'string\'';
    var mystring7 = "This is my \'string\'";
    var mystring8 = 'This is my \"string\"';

  • #3
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    I like to use single quotes.
    Reason: I find that within a string the " char is used far more often than the ' char, and switching to single quotes from double some years ago has greatly reduced my need for escaping \'. Also, I like to use .innerHTML to inject HTML in certain cases, and since I always use double quotes in declaring tag properties I never have to worry about mixing the two.

    This decision is based entirely on my own coding practices... If you use VBScript on the same page as JavaScript, or you use VBScript as your server-side language; you might prefer to use double quotes so as not to confuse yourself with the ' that is used for single line comments in VBScript...

    I think I agree that I have never heard either to be "best practice", and that you should do what makes your code easier for you to understand, and manage.
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Use whichever one will reduce or eliminate the number of quotes in the string that need to be escaped.

    That may mean that with two adjacent assignments that you use one around the first and the other around the second.

    You can even completely avoid the need to escape them by swapping mid string eg. ' " ' + " ' " is a single string containing space, double quote, space, space, single quote, space
    Last edited by felgall; 03-07-2012 at 02:10 AM.
    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.


  •  

    Posting Permissions

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