0

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

I'm trying to build a Node.js Express web application, and in the tutorial they use ' instead of " quite often, but there is no explanation why.

Can someone please explain the difference? Is this specific to JavaScript, or does it apply to other languages too?

Example:

app.configure('dev')

app.get("/", function (req, res)

Thanks :)

Community
  • 1
  • 1
  • My opinion: single quotes should always be used when creating a string literal in javascript (caveat: JSON). this is because it makes injected javascript from server side languages much less painful, because they usually use double quotes for their strings. – jbabey Dec 06 '12 at 18:23

2 Answers2

8

In JavaScript, both are equivalent. The only difference is that inside a single-quoted string you don't have to escape ", and vice versa:

'dev' === "dev"
'd"v' === "d\"v"
'd\'v' === "d'v"

Most other languages distinguish the two in some way. For example, in Bash and Perl, '' prevents variables from being expanded inside, so 'a$b' is the actual string a$b, whereas "a$b" is the string consisting of a plus the value of the variable b. In C, C++, C#, and Java, '' is used to create a single character constant, so that 'a' means the character a whereas "a" means a string containing that character.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ruakh
  • 175,680
  • 26
  • 273
  • 307
2

Javascript string literals can be enclosed with ' or "; there is no difference between them (except for nesting).
This is not true in most other languages.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • To expand just a little bit: There is no "common" meaning that JavaScript is violating here. Different languages do totally different things. Ruby and its predecessor interpolate double quoted strings and takes single quoted strings as literals; C uses double quotes for strings and single quotes for characters; Lisp uses double quotes for strings and single quotes for marking data not to be interpreted. – Chuck Dec 06 '12 at 18:02