Search code examples
node.jscoding-stylestandards

Why is it recommended not to use double quote unless we are writing JSON in Node.js Style?


I came across an interesting article. Which states unless until we are defining JSON we should use only single quote.

var foo = 'bar'; //Right way   

var foo = "bar"; //Wrong way

Can anyone put light on this, why is it so?

Any help greatly appreciated.


Solution

  • The most likely reason is programmer preference / API consistency.

    Strictly speaking, there is no difference in meaning; so the choice comes down to convenience.

    Here are several factors that could influence your choise:

    • House style: Some groups of developers already use one convention or the other.
    • Client-side requirements: Will you be using quotes within the strings? (See Ady's answer).
    • Server-side language: VB.Net people might choose to use single quotes for java-script so that the scripts can be built server-side (VB.Net uses double-quotes for strings, so the java-script strings are easy to distinguished if they use single quotes).
    • Library code: If you're using a library that uses a particular style, you might consider using the same style yourself.
    • When using single quotes, any apostrophe needs escaping. ('Joe\'s got a cool bike.') When using double quotes, they don't. ("Joe's got a cool bike.") Apostrophes are much more common in English strings than double quotes.
    • Personal preference: You might thing one or other style looks better.

    Please check following post that might be helpful for you When to Use Double or Single Quotes in JavaScript