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
  • 37,634
  • 51
  • 140
  • 215

1 Answers1

13

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
  • 187,712
  • 24
  • 229
  • 250