Glossary / Property access and optional properties

Property access and optional properties

Property access reads a key from an object using dot notation or computed bracket notation. A read searches own properties and then the prototype chain; if no property is found, it normally returns undefined. Optional-property handling must distinguish absence from a present undefined/null value when that distinction matters to the domain.

Access, presence, and side effects

const key = "status";
object.status; // fixed identifier key
object[key];   // computed key

key in object checks own and inherited properties. Object.hasOwn(object, key) checks only own properties. Reading a getter can execute arbitrary code, and proxy traps can customize property access. A null or undefined receiver throws unless optional chaining is used. Bracket keys are coerced to strings or Symbols; dynamic untrusted keys require careful validation.

For optional values, object?.field guards the receiver but does not verify the field’s type or presence. A missing field and a field explicitly containing undefined both read as undefined. Use a schema validator and presence checks where patch semantics distinguish “not supplied” from “set to undefined/null.” Prototype-chain behavior matters for dictionaries and can create security hazards if untrusted data is merged into ordinary objects.

Engineering practice

Prefer direct property access for known shapes and bracket access for intentional dynamic keys. Validate unknown inputs at boundaries. Use own-property checks when inherited values are not part of the contract. Avoid excessive optional chaining around required invariants because it can hide malformed data.

Interview questions

  1. Does in check own properties only? No; it includes inherited properties.
  2. How test for an own property? Object.hasOwn.
  3. Can reading a property execute code? Yes, through getters or Proxy traps.
  4. Does optional chaining validate the resulting property? No; it only guards a nullish receiver.

References