Glossary / Objects

Objects

An object is a mutable collection of properties. Property keys are strings or Symbols, and property values can be any JavaScript value, including other objects and functions. Objects are reference values: assigning one object to another variable copies the reference, so both variables can observe the same mutations. Objects underpin records, class instances, arrays, functions, dates, maps, and many host APIs.

Properties and lookup

A property can be a data property (value, writable) or an accessor property (get, set). Both have enumerable and configurable attributes. Object literal properties are usually writable, enumerable, and configurable. Property reads first search own properties, then walk the prototype chain. A getter may run code when a property is read; property access is not always a passive memory lookup.

const user = { name: "Ada", active: true };
const alias = user;
alias.active = false;
console.log(user.active); // false: both variables refer to one object

Dot notation names a fixed property (user.name); bracket notation evaluates a key (user[fieldName]). Non-string, non-Symbol keys are converted to strings, which can cause collisions (1 and "1"). Use Map when keys should retain arbitrary value identity.

Own properties, copying, and descriptors

Object.hasOwn(obj, key) tests for a direct property. The in operator also sees inherited properties. Object spread and Object.assign copy enumerable own properties shallowly; nested objects remain shared, getters may be invoked, and the prototype/descriptors are not preserved. Object.freeze prevents certain own-property changes but is shallow and does not freeze internal slots of every built-in.

For data records, prefer clear shapes and explicit validation. For untrusted dictionaries, use own-property checks and avoid unsafe merge patterns that can enable prototype pollution. Use Object.create(null) or Map when a dictionary should not inherit Object.prototype behavior.

Practical guidance

Separate immutable record-like values from mutable entities with lifecycle. Document ownership when objects cross API boundaries. Copy only the depth required. Do not assume JSON serialization preserves prototypes, Symbols, undefined, functions, or object identity.

Interview questions

  1. What does assignment do for an object? It copies a reference, not the object’s properties.
  2. What is the difference between in and Object.hasOwn? in includes inherited properties; Object.hasOwn checks only direct properties.
  3. Is object spread a deep clone? No. Nested object references are shared.
  4. What happens during a property read? Own-property lookup, then prototype lookup; an accessor getter may execute.
  5. When use Map instead of an object? For arbitrary key types, explicit map semantics, or dynamic dictionaries that should avoid prototype-key hazards.

References