What is the difference between double quotes and single quotes in Javascript?

Asked

Viewed 12,960 times

20

Context
In PHP, there is a difference between using single and double quotes. In this case, single quotes are for "strings common", and double quotation marks, used when processing string (concatenate, for example).

Question
Is there any difference between the use of " and ' for strings Javascript? Are there any changes in performance, or conflicts with use strict, for example?

Example

alert('foo'); // Aspas simples
alert("bar"); // Aspas duplas
  • 1

    In languages where single and double quotes are interchangeable I like to use a convention to say when using quotation marks of each type. A convention I like is single quotes for "internal" system strings and double quotes for things that will be shown to the user.

  • 1

    It’s a matter of taste even. Simple quotes make you save a shift.

  • Quotation marks of the same type can be used without generating any conflict, if you do so ""content"", the result will be this "content" in the output of a function, that is, the backslash with the quotation marks together make the browser identify them differently, even if they are equal.

3 answers

19


There are no differences from the programming point of view, even using Strict Mode ("use strict;")

As pointed out by Carlos André, one approach is that you can put simple quotes inside the string or the opposite:

alert('Ola "Mundo"');
alert("Ola 'Mundo'");

However, if you are using JSON, the strings in JSON use double quotes. Obviously, several libraries also support single quotes, but using single quotes for strings does not comply with JSON standards.

9

There is no difference.

The only advantage of using double quotes is that you can put single quotes inside the string or the opposite:

var str = "Lorem 'ipsum' dolor";
var str = 'Lorem "ipsum" dolor'; 

1

Although there is no rule when using the two types of quotation marks, the most common is the atributo herdar aspas duplas (") and the argumentos do mesmo aspas únicas ('), if you want it is possible to use the same type of quotation marks, without generating error something, just follow the model below.

\'conteudo\' or \"conteudo\" in this way the '\'a\'' will return in a function like 'a', without error, containing the same quotation marks as the attribute and its parameters. The use strict is not affected by such uses.

Example

<script>
var teste = { a: function(a){ console.log(a.name); } }
teste.a({name:'\'a\''});
</script>

Browser other questions tagged

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