最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Should I use double-quotes or single-quotes for quoting strings if I want to be consistent across multiple language

programmeradmin4浏览0评论

In JavaScript, it doesn't seem to matter whether you use single quotes or double quotes when writing strings. However, some programming languages treat them differently.

Is one more reliable than the other across multiple programming languages? Are there any pros or cons using one rather than the other (apart from when apostrophes or quotes are in the string)?

I would like to choose one and then be able to use it across multiple programming languages—with it meaning the same thing (if that's possible).

In JavaScript, it doesn't seem to matter whether you use single quotes or double quotes when writing strings. However, some programming languages treat them differently.

Is one more reliable than the other across multiple programming languages? Are there any pros or cons using one rather than the other (apart from when apostrophes or quotes are in the string)?

I would like to choose one and then be able to use it across multiple programming languages—with it meaning the same thing (if that's possible).

Share Improve this question edited Apr 11, 2009 at 23:28 eKek0 23.3k26 gold badges93 silver badges121 bronze badges asked Apr 11, 2009 at 22:28 Steve HarrisonSteve Harrison 126k17 gold badges89 silver badges72 bronze badges
Add a comment  | 

8 Answers 8

Reset to default 8

In a lot of scripting and shell languages, there is a significant difference between double and single quotes. For example, in Perl and a lot of Unix shells, when you use double quotes, the interpreter will parse through your string and try to do any substitutions for any variables in the string. If you use single quotes, the interpreter will not try to do anything fancy with the string; it will treat it as a plain literal.

In other languages like C++ or Java, double quotes are used for strings, and single for single character literals.

Every language might treat quotes differently. I think in Javascript it might not matter which one you use, but it would be best to do some research on the best practices, then just pick one methodology and be consistent. I've seen some examples where the person chose to use double quotes in HTML and single in Javascript, but I'm not sure if that's the "standard."

The meaning is completely dependent on the language you are using, so what you want is not possible.

Personally, I default to using double-quotes; it's not so much a matter of "cross-language consistency" as not having to think.

When I'm typing a string in whatever language, a double-quote works as a default in all languages I'm familiar with, but single quotes won't necessarily work.

Of course, if you're going have the string processed in some way (inline variable expansion, escaped characters or whatever) you'll need to think about and use whatever is appropriate for the language you're using.

But for the 90%+ case where "it's just a string, dammit", double quotes do the trick pretty much everywhere - at least for the languages I use. I know it's a small thing, but it's still one less thing I need to think about.

generally I use double quotes also in Javascript, because in the majority of the languages I usually program, strings are double quoted. in C/C++ for instance, single quote stands for a single character.

In some scripting languages (e.g., shell, Perl, Ruby, and PHP), single-quoted strings do not support character escapes (except, in some languages, to escape the quote and backslash characters) nor variable interpolation. Double-quoted strings support both.

Example in Perl:

my $name = 'Barney';
print "Hello, $name\n";   # "Hello, Barney" (followed by newline)
print 'Hello, $name\n';   # "Hello, $name\n"

So, if you want your JavaScript to be consistent with the above, you'd probably want to use single-quoted strings---but, character escapes are still supported in single-quoted strings in JS, but not in the other languages mentioned above.

If you're intended to move around multiple languages, getting too cosy with one or the other would probably be counter-productive. I do the majority of my development is C# (double quoted strings), but occasionally switch back to Delphi for Win32 stuff (single quoted strings). The longer I've been away from Delphi, the more mistakes I make. Strangely enough, when I'm working with both languages at once it's a little simpler!

As has been said previously, you'll have to deal with both if you do a lot of cross-language stuff, so best not to worry about it.

The meaning of quotes are very language dependant. Some languages also let you use `` (backquotes) to mean something completely different from the regular ' and " ` quotes.

in c the double quotes is used for set of characters.so it is used for strings. single quotes is used for to specify a single character.if the string is keep in a single quotes that is syntax error.

发布评论

评论列表(0)

  1. 暂无评论