1

I wonder if there is any difference between using "" or not with JS objects

With "": var object = { "prop1": 1 "prop2": 2 "prop3": 3 }

vs.

Without "": var object = { prop1: 1 prop2: 2 prop3: 3 }

aw04
  • 10,857
  • 10
  • 56
  • 89
ttback
  • 2,051
  • 5
  • 27
  • 40
  • duplicate of http://stackoverflow.com/questions/4348478/what-is-the-difference-between-object-keys-with-quotes-and-without-quotes – Jesse Kernaghan Jun 04 '15 at 18:04

3 Answers3

1

No difference from JavaScript point of view. These two declarations are equivalent to each other.

c-smile
  • 26,734
  • 7
  • 59
  • 86
1

Both would work most of the times.

The main issues with this are:

  • To produce valid JSON, you need to use double qoutes (even a single quote is techncially not standard).

  • Sometimes you would use property names that would require you to use qoutes. So for example "my-prop".

orcaman
  • 6,263
  • 8
  • 54
  • 69
0
var object = { "prop1": 1, "prop2": 2, "prop3": 3 }

The above object is a proper JSON.

var object = { prop1: 1, prop2: 2, prop3: 3 }

This is a simple javascript object.

Zee
  • 8,420
  • 5
  • 36
  • 58
  • @rodrigo-silveira. Both are _simple JavaScript objects_ but the second one is not _JSON._. – Zee Jun 04 '15 at 18:07
  • Actually, both objects in your example are simple JavaScript objects, and neither is proper JSON. "JSON is a **text format** that is completely language independent" (see http://json.org/), the proper way to represent your first example would be ```var jsonString = '{ "prop1": 1, "prop2": 2, "prop3": 3 }';``` – rodrigo-silveira Jun 04 '15 at 18:07
  • @rodrigo-silveira You are wrong, try your so called json [here](http://jsonlint.com/). The moment you enclose it in quotes it becomes a string. – Zee Jun 04 '15 at 18:09
  • If you try to parse your "proper JSON" object, you get a SyntaxError: ```JSON.parse({ "prop1": 1, "prop2": 2, "prop3": 3 })```. But if you convert that to a string first, it works as expected: ```JSON.parse('{ "prop1": 1, "prop2": 2, "prop3": 3 }');``` – rodrigo-silveira Jun 04 '15 at 18:11
  • @rodrigo-silveira You dont parse a JSON which is already a json, you parse a _json string_. – Zee Jun 04 '15 at 18:12
  • [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – rodrigo-silveira Jun 04 '15 at 18:15
  • @rodrigo-silveira where did you see me writing _JSON object_? What you have given is a simple string which has valid json inside it thats why we parse it to get the JSON. – Zee Jun 04 '15 at 18:18
  • @rodrigo-silveira The link you provided has no relevance here. Also instead of arguing simply try jsonlint.com. – Zee Jun 04 '15 at 18:20