I realized I've been switching between them with no understanding as to why, and am finding it hard to search for.
-
2Can you make a specific example? – Pekka Jun 01 '12 at 14:59
-
4Strings can use either `""` or `''`, it doesn't matter (pick the one that makes you need to escape less (`"Mr. O'Neil"` or `'I said "hi"'`). Numbers don't need to be quoted, eg. `12`. – gen_Eric Jun 01 '12 at 15:00
4 Answers
' '
and " "
are the same thing; they are used to define string literals.
Things without quotes can be an identifier, keyword, non-string literal, property name or a number (may have missed one).
Examples:
"hello world" literal (string)
'hello world' literal (string) with same contents
document identifier (object)
{ a: 1 } property name
if keyword (start conditional statement)
3.4 literal (number)
/abc/ literal (regex object)
String literals that are enclosed in single quotes don't need escaped double quotes and visa versa, e.g.:
'<a href="">click me</a>' HTML containing double quotes
"It's going to rain" String containing single quote

- 170,779
- 38
- 263
- 309
-
3The last two wouldn't really be considered identifiers, would they? Seems to me they're the literal syntax of their individual types (number and regular expression), just like the first two string examples. – Jun 01 '12 at 15:10
-
1@amnotiam you make a valid point; i've reclassified them as literals :) thanks! – Ja͢ck Jun 01 '12 at 15:14
-
1Ah, there we are. +1 Only other things (that I can think of) under the category of *stuff without quotes* would be operators and statement terminators... and I suppose whitespace if you want to count that. – Jun 01 '12 at 15:19
-
@amnotiam hehe quite right, but I think this will have to do for now unless OP wants a full language break down ;-) – Ja͢ck Jun 01 '12 at 15:22
-
' '
and " "
used to quote string literal and represents string(s) whereas literal without quote are variables (name of variable, constant) know as identifier, example
variable = 'Hello'; (Here `variable` is identifier and 'Hello' is string literal)
var = "Ho There"
You might question, what is the difference between ' (single quote)
and " (Double quote)
Difference is ,strings within "
if have special character then they need to escape. Example:
Variable = "hi " there"; ---> here you need to escape the "
inside string like
Variable = "hi \" there";
But if using, '
then no need of escaping (unless there is a extra '
in string). You can hve like
var = 'Hello " World"';

- 76,197
- 13
- 71
- 125
"
and '
are interchangeable (but need to be used together).
myObject["property"]
and myObject.property
are also interchangeable. $var foo = "property"; myObject[foo]
as well (per comment below).

- 59,258
- 35
- 162
- 290
-
`myObject[property]` gets the property of myObject named with the value of property – pbfy0 Jun 01 '12 at 15:07
A quick jsfiddle around and both single and double quotes escape control codes etc.
In latter days I have had errors from HTML where double quotes have not been used, and if you look at the spec for JSON you'll note it is a double quote that is asked for when quoting string literals. So it is double quotes that is the convention I think, for historical reasons.
However! In these days of writing server side JS I must admit I tend to be pulled back to my C roots and double quote where I want escaped chars and single quote strings that are effectively literal chars and never likely to contain escaped chars (even though this is essentially non-productive behaviour). Besides which most of my JS is coffeescript nowadays anyway, nobody ever wrote javascript for elegance, CS is a different kettle of fish though.

- 3,095
- 5
- 23
- 28