Possible Duplicate:
What is the difference between object keys with quotes and without quotes?
What is the difference between a and b as below?
var a = {foo : "bar"};
var b = {"foo" : "bar"};
Possible Duplicate:
What is the difference between object keys with quotes and without quotes?
What is the difference between a and b as below?
var a = {foo : "bar"};
var b = {"foo" : "bar"};
Both are valid JavaScript object literals, and evaluate to different objects with properties called foo
, with a.foo == b.foo
being true.
Since you tagged this json, the first statement is invalid JSON because keys need to be strings (aside from the var a
declaration).
There is no difference.
A key in an object literal can be an identifier or a string literal. You can use characters in a string that you can't use in an identifier, but foo
doesn't contain any of those.
(As an aside, if you were writing JSON rather than JS, then the key would have to be a string)
They are the same.
The quoted syntax allows to set keys that are not valid identifiers (like foo bar
) or that are reserved keywords (like for
).
JSON only allows the quoted syntax.