Using single or double quotes in a text string


What differences are there when using single or double quotes for text strings in JavaScript?

 var daw = "Desarrollo de aplicaciones web";
 var dam = 'Desarrollo de aplicaciones multiplataforma';
asked by Eduardo 13.10.2017 в 17:16

3 answers


It is a subject of tastes, since there is no difference in meaning. Douglas Crockford, one of the most well-known gurus of Javascript and developer of JSLint, prefers the use of double quotes. But many developers find it more comfortable otherwise.

Double quotes:

Pros :

  • It is what the JSON notation uses, with what many prefer it for familiarity / homogeneity:

    var obj={ "clave": "valor", "otra clave": 0};

  • The Anglo-Saxons find it convenient not to have to "escape" the apostrophes when using English:

    var text="don't escape this" .

Cons :

  • Precisely when trying to create a JSON, you have to escape the double quotes:

    var json="{\"clave\": 0}";

  • The same happens when wanting to generate HTML:

    var input="<input name=\"nombre\"/>";

Single quotes:

Reverse the pros and cons of simple ones.

Personally I always use single quotes, because I rarely use them as part of my texts, and I leave the doubles for HTML and JSON 1 , but you have to do what you find most comfortable.

In a project where you are working with other developers, I suggest creating a document that is the style book of the project (if it does not already exist, many companies have it defined at the corporate level) where it is established what type of quotation marks they will be used, the type of indentation (tabulation or spacing and in what quantity), the type of comments to be used, the maximum length of a line of code ... etc. to have a homogeneous code.

1 It never hurts to emphasize that the concept of "JSON object" does not exist, there are Javascript objects and strings in JSON format / notation that represent an object. "A JSON" is a string from which a Javascript object can be obtained through JSON.parse.

answered by 13.10.2017 / 17:43

No difference.

But it depends on the use you want to give the chain.

If it should contain double quotes, then you have to use single quotes

 var comillasDobles = 'contiene "dobles comillas" sin problemas';

and vice versa

 var comillasSimples= "contiene 'simples comillas' sin problemas";
Now if for some reason you have a variable you can do the following:

var texto = "pregunta";
var comillaDoble ='mi texto en comilla Doble es "${texto}"';
var comillaSimple ='mi texto en comilla Simple  es '${texto}'';
answered by 13.10.2017 в 17:20

It is indistinct to use double or single quotes.

It is necessary to consider that if you use a type of quotation marks and you want to reuse them within the chain, they must go with the escape symbol:

var cadena = "La siguiente \"palabra\" estaba entre comillas dobles y 'esta' con comillas simples.";
answered by 13.10.2017 в 17:27