Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS object keys with or without quotes? [duplicate]

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?

like image 992
Oto Shavadze Avatar asked Oct 20 '12 19:10

Oto Shavadze


1 Answers

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
}
like image 189
dfsq Avatar answered Sep 25 '22 08:09

dfsq