Is that according to me both could be used for any kind of thing like in strings in variables, cycles, etc. For example:
var comillasDobles = "Ejemplo";
var comillasSimples = 'Ejemplo dos';
According to me this was the same, but I have seen in many places that sometimes they say "your code is wrong because you used double quotes and they should be single" and vice versa. Could someone explain the difference to me? please.
Short answer, there is no difference.
Long answer, it depends on what you later want to use the chain for.
If it should contain double quotes, then you have to use single quotes
and backwards
if you have to contain both, then you have to escape with
\
one of them.Probably whoever recommended you to change the quotes must have referred to a case in which you contained those quotes inside the string. In particular, it is a typical advice for when you want to include html whose attributes go with double quotes (or vice versa)
or json, which forces you to use double quotes
and it is also usual to use, on the contrary, double quotes when the strings contain text in English or in other languages in which single quotes are used as an apostrophe, to avoid the hassle of having to use them all the time
\'
for what is much better to start the definitions with double quotes.instead of
Finally, to complete the answer, in the latest version of javascript (known as ES6 or ES2015 or ECMAScript 2015 or ECMA-262 6th Edition), there are reverse quotes (or grave accent or backtick)
`
that serve to delimit template literals but that they also serve (abusing them a bit) to declare strings.The initial answer is that in javascript, there is no difference .
You can (and should) use **both* when you find yourself in cases like these:
How do you see when you want to use marks within a text or definition that opens by
'
or"
uses the other within to differentiate.You could also use only one of them :
Using the slash
\
, what you do is eliminate the syntax that it has to open or close quotation and you simply take its value as a char.Another case in which it is more important to use both is so that there are no runtime problems in other languages , like
html
:As you can see, it is important to differentiate between single and double quotes in this case, although it can also be done:
Which is what I mentioned before
\
.Specifically when
html
you make calls in codejavascript
that contain calculated parameters (in agrid
for example) it is important to distinguish between single quotes and double quotes.Another important thing, from my personal point of view, is to use double quotes
javascript
whenever you can when defining strings since:If we do it with single quotes...
We have to use
\
it since we are already using the single quote in its definition and since the single quote is a typical character in English, it is annoying to have to use it all the time\'
because it is much better to start the definitions with double quotes.