Javascript Javascript中单引号和双引号的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3149192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:27:35  来源:igfitidea点击:

Difference between single quotes and double quotes in Javascript

javascriptquotes

提问by Michael Lumbroso

I know that in PHP, the only difference between double quotes and single quotes is the interpretation of variable inside a string and the treatment of escape characters.

我知道在 PHP 中,双引号和单引号之间的唯一区别是字符串内部变量的解释和转义字符的处理。

In JavaScript, I often see double quotes used in strings. Is there a particular reason for that, or are single quotes exactly the same as double quotes?

在 JavaScript 中,我经常看到字符串中使用双引号。是否有特殊原因,或者单引号与双引号完全相同?

回答by karim79

You'll want to use single quotes where you want double quotes to appear inside the string (e.g. for html attributes) without having to escape them, or vice versa. Other than that, there is no difference.

如果您希望双引号出现在字符串中(例如对于 html 属性)而不必转义它们,您将需要使用单引号,反之亦然。除此之外,没有任何区别。

However, note that JSON (JavaScript Object Notation) only supports double quoted strings.

但是,请注意 JSON(JavaScript Object Notation)仅支持双引号字符串。

回答by Jesse Brown

There is a difference in JSON - The JSON standard specifies that all key,value pairs should be in double quotes. (thanks to wulfgarpro in the comments), so I have started switching to using double-quotes as much as possible so that I don't make mistakes when dealing with JSON.

JSON 有所不同 - JSON 标准指定所有键值对都应使用双引号。(感谢 wulfgarpro 在评论中),所以我开始尽可能多地使用双引号,这样我在处理 JSON 时就不会出错。

回答by jAndy

Absolutly no difference. FREE QUOTING YEEHHAAA

完全没有区别。免费引用 YEEHHAA

回答by Alex.K.

Unlike PHP, for which using double or single quotes changes how the string is interpreted, there is no difference in the two syntaxes in ECMAScript. A string using double quotes is exactly the same as a string using single quotes. Note, however, that a string beginning with a double quote must end with a double quote, and a string beginning with a single quote must end with a single quote.

与使用双引号或单引号会改变字符串解释方式的 PHP 不同,ECMAScript 中的两种语法没有区别。使用双引号的字符串与使用单引号的字符串完全相同。但是请注意,以双引号开头的字符串必须以双引号结尾,以单引号开头的字符串必须以单引号结尾。

Nicholas C. Zakas - Professional JavaScript for Web Developers

Nicholas C. Zakas - 面向 Web 开发人员的专业 JavaScript

回答by Pharabus

They are the same, I usually use single quotes but thats because I am a .net developer and asp.net in particular so it aids me in distinguishing between the 2 types of strings.

它们是相同的,我通常使用单引号,但那是因为我是 .net 开发人员,特别是 asp.net,所以它帮助我区分两种类型的字符串。

回答by Sam

I just found a difference. I'm making a mobile website, but I've mostly been testing on desktop Firefox. This works fine on Firefox:

我刚刚发现了一个区别。我正在制作一个移动网站,但我主要是在桌面 Firefox 上进行测试。这在 Firefox 上运行良好:

var searchArray = searchValue.split(' '); // Split a string at the spaces.

BUT... it doesn't work on mobile Safari (iPhone 3GS running iOS 6.1). To make it work on mobile Safari, you have to use double quotes:

但是...它不适用于移动 Safari(运行 iOS 6.1 的 iPhone 3GS)。要使其在移动 Safari 上运行,您必须使用双引号:

var searchArray = searchValue.split(" "); // Split a string at the spaces.

If you don't use double quotes, it doesn't split, it just puts the whole string into the first array element. That was a real puzzler for me and took quite a while to figure out; I dunno what even made me try switching the quotes, because I thought they were always supposed to act the same way. I haven't found anything on this problem by googling, so maybe this will help someone.

如果不使用双引号,它不会拆分,它只是将整个字符串放入第一个数组元素中。这对我来说是一个真正的难题,花了很长时间才弄明白。我不知道是什么让我尝试切换引号,因为我认为它们总是应该以相同的方式行事。我没有通过谷歌搜索找到关于这个问题的任何东西,所以也许这会对某人有所帮助。