简体   繁体   English

JavaScript 中的 ' 和 " 有什么区别?

[英]What is the difference between ' and " in JavaScript?

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?我应该注意它们之间的任何显着差异吗?

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.除此之外,一切都一样。

Although not technically a difference in Javascript, its worth noting that single quoted strings are not valid JSON , per se.尽管在技术上与 Javascript 没有区别,但值得注意的是,单引号字符串本身不是有效的 JSON 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 .我认为人们会自动假设,因为 JSON 是有效的 JS,所以有效的 JS 字符串也是有效的 JSON,这不一定是真的

Eg, {'key': 'Some "value"'} is not valid JSON, whereas {"key": "Some 'value'"} is.例如, {'key': 'Some "value"'}不是有效的 JSON,而{"key": "Some 'value'"}是。

There's no difference.没有区别。 The reason for its existence is exactly what you mentioned它存在的原因正是你所说的

根据 Mozilla 的说法,好的做法是在 HTML 中使用“”(不能使用“”),而在 Javascript 中保留“”(“”和“”都可以无差别地使用)......

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.对于 str1 和 str4,document.write 将失败。 That is the difference, but I don't know if there is a workaround to make them work.这就是区别,但我不知道是否有解决方法可以使它们起作用。

Try this:尝试这个:

console.log("mama+"mama"")

Output : Uncaught SyntaxError: missing ) 
after argument list

Now try:现在尝试:

console.log('mama+"mama"')

Output :  mama+"mama"

They are equivalent!它们是等价的! but sometimes you want to use "" inside a string, and that why we have '但有时你想在字符串中使用 "",这就是为什么我们有 '

As written above, there is no difference but for situation you need to use "/' inside a string.如上所述,没有区别,但对于需要在字符串中使用“/”的情况。

I think a better practice for situation you need to concatenate strings with variables is using a template strings: Price: ${price}, Amount: ${amount}. Total: ${price*amount}对于需要将字符串与变量连接起来的情况,我认为更好的做法是使用模板字符串: Price: ${price}, Amount: ${amount}. Total: ${price*amount} Price: ${price}, Amount: ${amount}. Total: ${price*amount}

That's way you can add " and ', and concatenate variables.这样您就可以添加 " 和 ',并连接变量。

Much easier to read, much easier to write.更容易阅读,更容易写。

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.当您使用两种不同类型的引号时,数组会变得混乱。

Example:例子:

//WILL NOT WORK
var array = ["apple","orange","banana"];

array.push('pear');

//WILL WORK
var array = ["apple","orange","banana"];

array.push("pear");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM