Glossary / Computed properties

Computed properties

Computed property names evaluate an expression to determine a property key. They are used in object literals, class elements, and bracket access when the key is dynamic or is a Symbol. Object property keys ultimately are strings or Symbols; other values are converted to strings.

Dynamic object construction

const field = "status";
const symbolKey = Symbol("metadata");
const result = {
  [field]: "ready",
  [symbolKey]: { source: "cache" },
  ["get" + "Value"]() { return this[field]; },
};

The expression inside brackets is evaluated once when the literal/class definition is evaluated. Computed method names still use ordinary method-call this behavior. Numeric keys become string property names (obj[1] addresses "1"); Symbols remain Symbols. This coercion means keys such as 1 and "1" collide in a plain object. Use Map if arbitrary values must remain distinct keys.

Dynamic keys also create a trust boundary. Assigning untrusted keys into objects can interact with inherited properties or prototype mutation mechanisms, depending on the merge code. Use schema validation, safe dictionary structures, and safe merge utilities. Computed naming itself does not sanitize or validate a key.

Interview questions

  1. What types can object property keys have? Strings and Symbols.
  2. What happens to a numeric key? It is converted to a string property key.
  3. Why use Map for arbitrary keys? It preserves key type and identity rather than coercing keys to strings.
  4. Can a computed method use this? Yes; it behaves like a normal method property when called through its receiver.

References