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

发布于 2024-09-07 06:31:31 字数 110 浏览 3 评论 0原文

我知道在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦纸 2024-09-14 06:31:33

如果您希望双引号出现在字符串内(例如,对于 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.

盛装女皇 2024-09-14 06:31:33

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.

⊕婉儿 2024-09-14 06:31:33

绝对没有区别。免费引用耶哈哈

Absolutly no difference. FREE QUOTING YEEHHAAA

相思碎 2024-09-14 06:31:33

与 PHP 不同,PHP 使用双引号或单引号会改变
字符串被解释,两种语法没有区别
ECMAScript。使用双引号的字符串与
使用单引号的字符串。但请注意,字符串开头
带双引号的必须以双引号和字符串结尾
以单引号开头必须以单引号结束。

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

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.

Nicholas C. Zakas - Professional JavaScript for Web Developers

孤独患者 2024-09-14 06:31:33

它们是相同的,我通常使用单引号,但那是因为我是 .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.

农村范ル 2024-09-14 06:31:33

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

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

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

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

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

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:

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文