What are the differences when using single or double quotes for text strings in JavaScript?
var daw = "Desarrollo de aplicaciones web";
var dam = 'Desarrollo de aplicaciones multiplataforma';
What are the differences 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 matter of taste, since there is no difference in meaning. Douglas Crockford , one of the best known Javascript gurus and developer of JSLint, prefers the use of double quotes. But many developers find the opposite more comfortable.
Double quotation marks:
Pros :
It is what the JSON notation uses, with which many prefer it for familiarity/homogeneity:
var obj={ "clave": "valor", "otra clave": 0};
Anglo-Saxons find it convenient not to have to "escape" 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 the simple ones.
I personally always use single quotes, because I rarely use them as part of my texts, and I leave double quotes for HTML and JSON 1 code , but you have to do what you find most comfortable.
In a project in which you are working with other developers, I recommend 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 quotes are going to be used. to use, the type of indentation (tabulation or spacing and how much), the type of comments to use, the maximum length of a line of code... etc. to have a homogeneous code.
1 It is never too much 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 using JSON.parse.
reverse single quotes:
The last to come to Javascript are the quotes for string templates :
They have special features that single or double quotes don't have, such as being multi-line or accepting parameters in the
${valor}
.There's no difference.
But if it depends on the use you want to give the chain.
If it should contain double quotes, then you have to use single quotes
and backwards
The use of double or single quotes is indistinct.
You have to consider that if you use a type of quotes and you want to reuse them within the string, they must go with the escape symbol: