1

Why does JS allow object properties to be both "quoted" and non-quoted? Is there a difference?

var object = {

    "firstName": "SpongeBob", //quoted
    lastName: "SquarePants"   //non-quoted

};

console.log(object.firstName); // -> SpongeBob
console.log(object.lastName);  // -> SquarePants

1 Answers1

0

Sure there is:

var obj = {
    "crazy-property!name": "some string"
    // crazy-property!name is an invalid property name without quotation marks
};

console.log(obj["crazy-property!name"]);
// can't do console.log(obj.crazy-property!name)
Platinum Azure
  • 45,269
  • 12
  • 110
  • 134