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';
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';
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.
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\"/>";
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.
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';
console.log(comillasDobles)
and vice versa
var comillasSimples= "contiene 'simples comillas' sin problemas";
console.log(comillasSimples);
var texto = "pregunta";
var comillaDoble ='mi texto en comilla Doble es "${texto}"';
var comillaSimple ='mi texto en comilla Simple es '${texto}'';
console.log(comillaDoble);
console.log(comillaSimple);
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.";