16

Let's say I have the following object:

var VariableName = {
    firstProperty: 1,
    secondProperty: 2
}

Do I have to wrap the object properties in quotes like this?

var VariableName = {
    'firstProperty': 1,
    'secondProperty': 2
}

Is this Single quotes in JavaScript object literal the correct answer?

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
machineaddict
  • 3,216
  • 8
  • 37
  • 61
  • 4
    It may be interesting to note that whilst quotes *can* be omitted from names when using object literal notation, the JSON specification **requires** key names to **double** quoted. – Matt Feb 08 '12 at 11:39

4 Answers4

36

No, you don't need to do that.

The only reasons to quote object-keys are

  • the property name is reserved/used by the browser/js engine (eg. "class" in IE)
  • you have special characters or white spaces in your key

so for instance

var VariableName = {
    "some-prop": 42,  // needs quotation because of `-`
    "class": 'foobar' // doesn't syntatically require quotes, but it will fail on some IEs
    valid: 'yay' // no quotes required
};
jAndy
  • 231,737
  • 57
  • 305
  • 359
  • 5
    This answer isn’t really accurate. It doesn’t explain what those “special characters” are exactly (and it doesn’t link to such an explanation), and it only mentions the ES3 behavior where reserved words aren’t allowed to be used as unquoted property names — in ES5 this is allowed. See my answer on a duplicate question here: http://stackoverflow.com/a/9568622/96656 – Mathias Bynens Mar 06 '12 at 07:47
6

You only have to use the quotes around the property, if the property name is a reserved word (like for, in, function, ...). That way you prevent Javascript from trying to interpret the keyword as a part of the language and most probably get a syntax error. Furthermore, if you want to use spaces in property names, you also have to use quotes. If your property names are just normal names without any collusion potential or spaces, you may use the syntax you prefer.

One other possibility that requires quotes is the use of Javascript minifiers like google closure compiler, as it tends to replace all property names. If you put your property names in quotes, however, the closure compiler preserves the property as you coded it. This has some relevance when exporting objects in a library or using an parameter object.

Sirko
  • 72,589
  • 19
  • 149
  • 183
5

Property names in object literals must be strings, numbers or identifiers. If the name is a valid identifier, then you don't need quotes, otherwise they follow the same rules as strings.

firstProperty and secondProperty are both valid identifiers, so you don't need quotes.

See page 65 of the specification for more details.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

For Javascript you usually do not have to use quotes. You may use ' or " if you like, and you must use quotes if there is a clash between the name of your property and a JS reserved word like null. The answer you linked to seems to be correct, yes.

For JSON, you should use " around strings (including object property names)

Matthew Gilliard
  • 9,298
  • 3
  • 33
  • 48