-1

I am very new to javascript. We can write json as follows

const obj1 = {key: "myKey", value: "myValue"}  
const obj2 = {"myKey": "myValue"}

what is a difference between these two? And when should I prefer using one over another?

js_248
  • 2,032
  • 4
  • 27
  • 38
  • There is definitely a difference. Please ignore all the answers below saying otherwise, and instead read the second answer to the duplicate question. – 4castle Jul 28 '17 at 06:34

3 Answers3

-1

The first one is the JavaScript way. The second one would be JSON format. You can use both in JavaScript. My preference is the first way.

JSON Derulo
  • 9,780
  • 7
  • 39
  • 56
-1

As far as I know, it doesn't matter. It's up to you.

Krzysztof
  • 19
  • 2
-1

There isn't a difference. Write however you want. I personally prefer the key not to be a string.var a = {key: "value"}

  • Strictly speaking there is a difference: https://stackoverflow.com/questions/4201441/is-there-any-practical-reason-to-use-quoted-strings-for-json-keys – rrd Jul 28 '17 at 06:37