Use single or double quotes for strings in Javascript?

Asked

Viewed 2,563 times

3

Is there any difference between writing a literal string between single and double quotes?

Example:

var s = 'texto';
// ou
var s = "texto";
  • Best practice based on what?

  • 1

    Related (Soen): http://stackoverflow.com/questions/242813/

  • Okay, thanks for the tip.

  • 2

    @Marcellalves I gave a "pat" on your question. If you do not like, you can reverse the edition, then I will leave my downvote by citing "best practice" without any context. Note. about editing: I took the second example pq string is always string, it makes no difference where it is used.

1 answer

4


It’s a matter of taste. They make no difference in Javascript.

There may be cases where one is more practical, for example:

var stringA = 'Olá Mc\'Neil';
var stringB = "Olá Mc'Neil";

console.log(stringA, '|', stringB); // Olá Mc'Neil | Olá Mc'Neil

In other languages like PHP is different.

  • but there is the question of consumption?

  • @Magichat better explains what you mean by "consumption"? you mean what I added later in the example?

  • 1

    for example in this answer (http://answall.com/questions/4652/diff%C3%A7a-between-quotes-single-and-double-in-php#4680) it is stated that in php there is a different memory consumption, I do not test to know if it is real, however if it happens tmb in js ?

  • That was one of my doubts.

  • 1

    @Magichat that I know compilers do not do different treatment. Taking a look at posts that talk about this subject (this, this, or reading directives of Ecmascript) both are treated as equal.

  • To me what you say is a reference in js, so ok ...

  • @Magichat :) It’s always good to question. I don’t know everything.

  • ah yes, but in js it seems that if it is almost there heuheuehu

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.