JavaScript中单引号和双引号的区别
我知道在PHP中,双引号和单引号之间的唯一区别是字符串内变量的解释和转义字符的处理。
在 JavaScript 中,我经常看到字符串中使用双引号。是否有特殊原因,或者单引号与双引号完全相同?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您希望双引号出现在字符串内(例如,对于 html 属性),则需要使用单引号,而不必转义它们,反之亦然。除此之外,没有什么区别。
但请注意,JSON(JavaScript 对象表示法)仅支持双引号字符串。
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.
However, note that JSON (JavaScript Object Notation) only supports double quoted strings.
JSON 中存在差异 - JSON 标准指定所有键、值对都应使用双引号。 (感谢评论中的 wulfgarpro),所以我开始尽可能多地使用双引号,这样我在处理 JSON 时就不会犯错误。
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.
绝对没有区别。免费引用耶哈哈
Absolutly no difference. FREE QUOTING YEEHHAAA
Nicholas C. Zakas - Web 开发人员的专业 JavaScript
Nicholas C. Zakas - Professional JavaScript for Web Developers
它们是相同的,我通常使用单引号,但那是因为我是 .net 开发人员,特别是 asp.net,因此它可以帮助我区分这两种类型的字符串。
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.
我刚刚发现了一个不同之处。我正在制作一个移动网站,但我主要是在桌面版 Firefox 上进行测试。这在 Firefox 上运行良好:
但是...它不适用于移动 Safari(运行 iOS 6.1 的 iPhone 3GS)。要使其在移动 Safari 上工作,您必须使用双引号:
如果不使用双引号,则它不会拆分,它只是将整个字符串放入第一个数组元素中。这对我来说确实是一个难题,我花了很长时间才弄清楚。我不知道是什么让我尝试更换引号,因为我认为它们总是应该以相同的方式行事。我还没有通过谷歌搜索找到任何关于这个问题的信息,所以也许这会对某人有所帮助。
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:
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:
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.