3

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.

Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164

2 Answers2

1

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

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

First of all this is just a style guide.

You can define your ECMAScript strings the way you like them. It is syntactically correct to use single quotes or double quotes for strings.

But according to JSON Specifications, a JSON value can be a string in double quotes, or a number, or true or false or null, or an object or an array.

fardjad
  • 20,031
  • 6
  • 53
  • 68