JavaScript 字符串的区别

发布于 2024-09-02 20:55:21 字数 668 浏览 3 评论 0原文

可能的重复:
js 中的单引号与双引号
何时在 JavaScript 中使用双引号或单引号 < /p>

何时 下面定义的 javascript 字符串之间有什么区别(如果有的话)?

var str1 = "Somestring";

var str2 = 'Somestring';

“”和 '' 对我主要用 C++ 编写代码来说意味着两个非常不同的事情:-)

编辑:如果没有区别,为什么有两种方法可以实现相同的事情,并且哪种方法被认为是更好的实践以及原因。谢谢!

Possible Duplicates:
single quotes versus double quotes in js
When to Use Double or Single Quotes in JavaScript

What is the difference (if any) between the javascript strings defined below?

var str1 = "Somestring";

var str2 = 'Somestring';

"" and '' mean two very different things to me predominantly writing code in C++ :-)

EDIT: If there is no difference why are there two ways of achieving the same thing and which is considered better practice to use and why. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

十级心震 2024-09-09 20:55:21

JavaScript 将单引号和双引号视为字符串分隔符。

如果使用单引号,则可以在字符串内使用双引号而不转义它们。

如果使用双引号,则可以在字符串内使用单引号而不转义它们。

这两个示例评估的是同一件事。

alert(str1 == str2); // true
alert(str1 === str2); // true

为什么有两种方式?由于 javascript 允许您混合两者的方式,您可以编写 html 属性而无需混乱的转义:

var htmlString1 = "<a href='#'>link</a>";
var htmlString2 = '<a href="#">link</a>';

至于最佳实践,没有约定。使用感觉最好的。

就我个人而言,我喜欢确保我发出的 Javascript 与 HTML 匹配(如果我使用双引号属性,我将使用 ' 分隔 JS 字符串,因此发出的属性将使用 ") 。

Javascript treats single and double quotes as string delimiters.

If you use single quotes, you can use double quotes inside the string without escaping them.

If you use double quotes, you can use single quotes inside the string without escaping them.

Both examples evaluate to the same thing.

alert(str1 == str2); // true
alert(str1 === str2); // true

Why two ways? Due to the way javascript allows you to mix the two, you can write html attributes out without messy escapes:

var htmlString1 = "<a href='#'>link</a>";
var htmlString2 = '<a href="#">link</a>';

As for best practice, there is no convention. Use what feels best.

Personally, I like making sure the Javascript I emit matches the HTML (if I double quote attributes, I will delimit JS string with a ', so emitted attributes will use ").

述情 2024-09-09 20:55:21

在 Javascript 中,字符串是用单引号或双引号(' 或 ")括起来的零个或多个 Unicode 字符的序列。双引号字符可以包含在由单引号字符分隔的字符串中,单引号字符可以包含在 。

在客户端 JavaScript 编程中,JavaScript 代码通常包含 HTML 代码字符串,而 HTML 代码通常包含 JavaScript 代码字符串,与 JavaScript 一样,HTML 使用单引号或双引号来分隔其字符串 将 JavaScript 和 HTML 结合起来,最好对 JavaScript 使用一种引号样式,对 HTML 使用另一种引号样式。

In Javascript a string is a sequence of zero or more Unicode characters enclosed within single or double quotes (' or "). Double-quote characters may be contained within strings delimited by single-quote characters, and single-quote characters may be contained within strings delimited by double quotes.

In client-side JavaScript programming, JavaScript code often contains strings of HTML code, and HTML code often contains strings of JavaScript code. Like JavaScript, HTML uses either single or double quotes to delimit its strings. Thus, when combining JavaScript and HTML, it is a good idea to use one style of quotes for JavaScript and the other style for HTML.

月隐月明月朦胧 2024-09-09 20:55:21

完全没有区别。

No difference at all.

猫腻 2024-09-09 20:55:21

我相信答案是没有区别。它们都是字符串。

的用法,

var mynewhtml = '<body class="myclass" ></body>';

这里是 '或 using "

var mynewhtml = "<body class='myclass' ></body>";

这也可以,但 IMO 更难阅读

var mynewhtml = "<body class=\"myclass\" ></body>";

I believe the answer is there is no difference. They are both strings.

Here would be the usage of '

var mynewhtml = '<body class="myclass" ></body>';

or using "

var mynewhtml = "<body class='myclass' ></body>";

this also works but IMO is harder to read

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