7

I'm not sure of the exact wording to use, but I have seen object assignments in javascript done two wasy

$('#test').dataTable({ fnInitComplete: myFunction });

and

$('#test').dataTable({ "fnInitComplete": myFunction });

Is there any actual difference between these, or any gotchas to be aware of?

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
KallDrexx
  • 27,229
  • 33
  • 143
  • 254
  • 1
    possible duplicate of [In JavaScript, what is the difference between a property name in double-quotes ("") and without?](http://stackoverflow.com/questions/4534404/in-javascript-what-is-the-difference-between-a-property-name-in-double-quotes) and [Single quotes in JavaScript object literal](http://stackoverflow.com/questions/2788236/single-quotes-in-javascript-object-literal) and [JSON syntax for property names](http://stackoverflow.com/questions/380855/json-syntax-for-property-names) – Felix Kling Nov 02 '11 at 19:27

4 Answers4

10

There is no difference.

However, if the key is not a valid identifier (eg, it's a keyword, or it has spaces or punctuation), quotes are required.

Also, the JSON standard (which is not Javascript) always requires double-quotes.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • +1: Good to know. I was wondering the same thing myself. Does this apply across the board with jQuery? – James Johnson Nov 02 '11 at 19:29
  • 1
    @JamesJohnson: This is Javascript syntax. It has nothing to do with jQuery. – SLaks Nov 02 '11 at 19:29
  • 1
    get used to Double quotes. As @SLaks says, the JSON standard is double quotes. When handling JSON on the server (with Java or whatever), most libraries that parse JSON for you are going to want to see double quotes. So, just because Javascript JSON doesn't care, other JSON engines will. – frosty Nov 02 '11 at 19:34
  • I was wondering speficially about using JSON to set properties using jQuery, like when adding attributes for example. – James Johnson Nov 02 '11 at 19:35
  • @JamesJohnson: That's still Javascript syntax. However, **strings** passed to `$.parseJSON` must be valid JSON. – SLaks Nov 02 '11 at 19:41
  • 1
    @aaronfrost: There is nothing such as "JavaScript JSON". JavaScript object literal syntax and JSON are not the same. – Felix Kling Nov 02 '11 at 19:42
2

The currently accepted answer is incorrect:

However, if the key is not a valid identifier (eg, it's a keyword, or it has spaces or punctuation), quotes are required.

The quotes aren’t required if you use a numeric literal as a property name. Also, keywords and reserved words are valid identifier names, and all identifier names (not just identifiers) are valid JavaScript property names.

From Unquoted property names / object keys in JavaScript, my write-up on the subject:

Quotes can only be omitted if the property name is a numeric literal or a valid identifier name.

[…]

Bracket notation can safely be used for all property names.

[…]

Dot notation can only be used when the property name is a valid identifier name.

I also made a tool that will tell you if any given property name can be used without quotes and/or with dot notation. Try it at mothereff.in/js-properties.

Screenshot

Community
  • 1
  • 1
Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
1

The main difference is that with quotes, you can use keys with spaces, js keywords, etc that are illegal as normal symbols. That's why JSON requires them.

rob
  • 9,933
  • 7
  • 42
  • 73
  • Umm, what does the J stand for? Regardless, I never implied it was, but it IS a subset of javascript, and the fact that you can, for instance, paste JSON into a js file and have it work is one of the benefits of JSON. The fact that you have to make sure keys are quoted if you go the other direction is a caveat, and possibly a reason to standardize on using quotes for keys on your object literals. Anyway, the JSON comment was an aside, relevant to some I'd think. – rob Nov 02 '11 at 19:36
  • Ok, I just was confused that you brought JSON in (and made this conclusion). – Felix Kling Nov 02 '11 at 19:40
0

Without the quotes, the property name must be either a number, or a valid JavaScript identifier. With the quotes, you can use an arbitrary string. Quoting a string that's already a valid JS identifier is functionally identical to using just the identifier.

millimoose
  • 39,073
  • 9
  • 82
  • 134