10

Possible Duplicate:
What is the difference between object keys with quotes and without quotes?

Is interest, what is right way, write object keys in quotes or not? that is

var obj = {
    "name": "Jhon"
}

or

var obj = {
    name: "Jhon"
}

for example, from php code echo json_encode(array("a"=>"aaa","b"=>"bbb")); result is object who has keys with quotes. But for example see jquery animate, in documentation is keys without quotes, (This is also JS object format, right?)

                $("#someElement").animate({
                    marginLeft: "200px"
                },
                {
                    duration: 1000
                });

So, what is more right way?

Community
  • 1
  • 1
Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236

1 Answers1

14

Don't confuse JSON object and Javascript object literal. JSON object is basicly just a string and its syntax requires to have proper quotes. However for javascript object quotes around its properties are not necessary. But in some cases you have to use them, e.g:

var test = {
    "with spaces": 12
}
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • 1
    O, right I confuse JSOn and JS objects. As I understood, in any case, better is write keys in quotes, so, thanks. – Oto Shavadze Oct 20 '12 at 19:59
  • 3
    No, better to not use quotes. Realy. You only need these extra characters in case if property name is not valid identifier: "with space", "hello-john", "123test", etc. – dfsq Oct 20 '12 at 20:02