Is there a difference between quoted and un-quoted JavaScript object property/method names?
For example, what is the difference between these two:
var obj1 = {
property1 : "Value 1",
method1 : function() {
return true;
}
};
var obj2 = {
"property1" : "Value 1",
"method1" : function() {
return true;
}
};
There is no difference in JavaScript. However you will have to quote property names that happen to be reserved words (such as class
), or names that contain invalid characters (such as first-name
).
Up until ES 3 you need to quote reserved words of the language (new, default, class, etc.). In the new version, however, this will be unnecessary.
But since ES 5 is not well-supported yet, you need to stick with quoting all reserved words.
If you don't want to memorize the full list of words, you better off with quoting everything.
Extra: this is why you don't have float
and class
properties on an element. You have to use cssFloat/styleFloat
and className
instead.
Another addition is that you need to quote every key in a JSON string. The reason is because they wanted it to be language independent, in order not to interfere with stupid restrictions like the one in ES3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With