0

If you have an object, for example:

const myObject = {
    "MyValue":5
}

or

const myObject = {
    MyValue:5
}

would there be a difference between using myObject["MyValue"] and myObject.MyValue?

Also, would the "" around the keys in the object make a difference as well? Are they both just preferences?

  • no there is no difference. `myObject["MyValue"]` is usually used if you have an variable and want to access some value from the object like `myObject[myVariable]` because this wont work `myObject.myVariable` – bill.gates Jun 01 '22 at 14:41
  • 1
    @bill.gates no difference *in this code*. There are differences in other cases. – VLAZ Jun 01 '22 at 14:42
  • 1
    Sometimes object keys have special characters which mean other things in javascript. So myObject["test-value"] would be fine, whereas myObject.test-value would be an error (it would try to evaluate myObject.test minus value). – James Jun 01 '22 at 14:44

0 Answers0