I saw this question and I am wondering about the same thing in JavaScript.
我看到了这个问题,我想知道JavaScript中的同样的事情。
If you use the character ' or the character " when making strings in JavaScript, the application seems to behave the same. So what is the difference between these two characters?
如果在JavaScript中创建字符串时使用字符'或字符',则应用程序似乎表现相同。那么这两个字符之间有什么区别?
The only advantage I have seen in using ' to build strings is that I can do stuff like:
我在使用'构建字符串时看到的唯一优势是我可以做以下事情:
var toAppend = '<div id="myDiv1"></div>';
Instead of:
代替:
var toAppend = "<div id=\"myDiv1\"></div>";
Is there any significant difference between them that I should be aware of?
我应该注意到它们之间是否有任何显着差异?
8 个解决方案
#1
68
They are equivalent for all intents and purposes. If you want to use either one inside a string, it is a good idea to use the other one to create the string, as you noted. Other than that, it's all the same.
它们等同于所有意图和目的。如果你想在字符串中使用任何一个字符串,最好使用另一个来创建字符串,正如你所指出的那样。除此之外,它都是一样的。
#2
36
Although not technically a difference in Javascript, its worth noting that single quoted strings are not valid JSON, per se. I think that people automatically assume that since JSON is valid JS, that valid JS strings are also valid JSON, which isn't necessarily true.
虽然从技术上讲不是Javascript的差异,但值得注意的是单引号字符串本身并不是有效的JSON。我认为人们会自动认为既然JSON是有效的JS,那么有效的JS字符串也是有效的JSON,这不一定是真的。
E.g., {'key': 'Some "value"'}
is not valid JSON, whereas {"key": "Some 'value'"}
is.
例如,{'key':'Some'值“'}不是有效的JSON,而{”key“:”Some'value'“}是。
#3
17
There's no difference. The reason for its existence is exactly what you mentioned
没有区别。它存在的原因正是你提到的
#4
2
I think there is another difference. If you do the following
我认为还有另一个不同之处。如果您执行以下操作
var str1 = 'The \' character';
var str2 = 'The " character';
var str3 = "The ' character";
var str4 = "The \" character";
document.write (str1.replace("'","%26");
document.write (str2.replace('"',"%22");
document.write (str3.replace("'","%26");
document.write (str4.replace('"',"%22");
The document.write will fail for str1 and str4. That is the difference, but I don't know if there is a workaround to make them work.
对于str1和str4,document.write将失败。这是不同的,但我不知道是否有解决方法使它们工作。
#5
2
Good practice, according to Mozilla, is to use " " in HTML (where ' ' cannot be used) while reserving ' ' in Javascript (where both " " and ' ' can be use indifferently)...
根据Mozilla的说法,良好的做法是在HTML中使用“”(其中''不能使用),同时在Javascript中保留''(其中“和”可以无差别地使用)......
#6
0
Try This :
尝试这个 :
console.log("mama+"mama"")
Output : Uncaught SyntaxError: missing )
after argument list
Now Try :
现在尝试:
console.log('mama+"mama"')
Output : mama+"mama"
#7
-1
Your example is the best example to describe the difference between the two....
你的例子是描述两者之间差异的最好例子....
#8
-1
WARNING!!!!
警告!!!!
There is a difference. When adding to arrays, you have to use one or the other. The array gets confused when you use two different types of quotes.
它们是有区别的。添加到数组时,您必须使用其中一个。当您使用两种不同类型的引号时,数组会混淆。
Ex: //WILL NOT WORK var array = ["apple","orange","banana"];
例如://不会工作var array = [“apple”,“orange”,“banana”];
array.push('pear');
的Array.push( '梨');
//WILL WORK var array = ["apple","orange","banana"];
//将工作var array = [“apple”,“orange”,“banana”];
array.push("pear");
的Array.push( “梨”);
#1
68
They are equivalent for all intents and purposes. If you want to use either one inside a string, it is a good idea to use the other one to create the string, as you noted. Other than that, it's all the same.
它们等同于所有意图和目的。如果你想在字符串中使用任何一个字符串,最好使用另一个来创建字符串,正如你所指出的那样。除此之外,它都是一样的。
#2
36
Although not technically a difference in Javascript, its worth noting that single quoted strings are not valid JSON, per se. I think that people automatically assume that since JSON is valid JS, that valid JS strings are also valid JSON, which isn't necessarily true.
虽然从技术上讲不是Javascript的差异,但值得注意的是单引号字符串本身并不是有效的JSON。我认为人们会自动认为既然JSON是有效的JS,那么有效的JS字符串也是有效的JSON,这不一定是真的。
E.g., {'key': 'Some "value"'}
is not valid JSON, whereas {"key": "Some 'value'"}
is.
例如,{'key':'Some'值“'}不是有效的JSON,而{”key“:”Some'value'“}是。
#3
17
There's no difference. The reason for its existence is exactly what you mentioned
没有区别。它存在的原因正是你提到的
#4
2
I think there is another difference. If you do the following
我认为还有另一个不同之处。如果您执行以下操作
var str1 = 'The \' character';
var str2 = 'The " character';
var str3 = "The ' character";
var str4 = "The \" character";
document.write (str1.replace("'","%26");
document.write (str2.replace('"',"%22");
document.write (str3.replace("'","%26");
document.write (str4.replace('"',"%22");
The document.write will fail for str1 and str4. That is the difference, but I don't know if there is a workaround to make them work.
对于str1和str4,document.write将失败。这是不同的,但我不知道是否有解决方法使它们工作。
#5
2
Good practice, according to Mozilla, is to use " " in HTML (where ' ' cannot be used) while reserving ' ' in Javascript (where both " " and ' ' can be use indifferently)...
根据Mozilla的说法,良好的做法是在HTML中使用“”(其中''不能使用),同时在Javascript中保留''(其中“和”可以无差别地使用)......
#6
0
Try This :
尝试这个 :
console.log("mama+"mama"")
Output : Uncaught SyntaxError: missing )
after argument list
Now Try :
现在尝试:
console.log('mama+"mama"')
Output : mama+"mama"
#7
-1
Your example is the best example to describe the difference between the two....
你的例子是描述两者之间差异的最好例子....
#8
-1
WARNING!!!!
警告!!!!
There is a difference. When adding to arrays, you have to use one or the other. The array gets confused when you use two different types of quotes.
它们是有区别的。添加到数组时,您必须使用其中一个。当您使用两种不同类型的引号时,数组会混淆。
Ex: //WILL NOT WORK var array = ["apple","orange","banana"];
例如://不会工作var array = [“apple”,“orange”,“banana”];
array.push('pear');
的Array.push( '梨');
//WILL WORK var array = ["apple","orange","banana"];
//将工作var array = [“apple”,“orange”,“banana”];
array.push("pear");
的Array.push( “梨”);