Objects
An object is a collection of named properties. It is the container JavaScript uses for almost everything that is not a plain number, string, or boolean, and it is always handled by reference.
Detailed Explanation
An object is a bag of key-value pairs. The key is called a property name, the value can be anything, including another object or a function. If you are modelling a user, the object is the natural shape: { id: 7, email: "ada@example.com", isActive: true }.
Property keys are only ever strings or symbols. That surprises people. Write scores[1] = 90 and the key stored is the string "1", because the number is coerced. Symbols exist so a library can attach a property that will never collide with yours.
The important behaviour is reference semantics. A variable holding an object holds a reference to it, not a copy. Assigning that variable to another name gives two names for one object, so a change through either name is visible through both. Equality follows the same rule: {} === {} is false because those are two different objects, even though they look identical. Comparing objects by content is something you write yourself.
There are three broad ways to make one. An object literal, {}, is the everyday choice. Object.create(proto) makes an object with a prototype you choose, including Object.create(null) for a dictionary with no inherited keys at all. A constructor or class produces objects with shared behaviour. All three produce the same kind of thing.
Every object also has a hidden link to another object, its [[Prototype]]. That link is why a fresh {} already answers to toString and hasOwnProperty: those live on Object.prototype, not on your object. Properties you put on the object yourself are called own properties, and most inspection tools only report those.
Property order is defined, not random. Integer-like keys come first in ascending numeric order, then string keys in insertion order, then symbol keys. That is why { 2: "b", 1: "a" } enumerates 1 before 2.
Reading a missing property is not an error. It returns undefined. This is convenient and also the source of many bugs, because user.adress.city throws only on the second step, once undefined is the thing being indexed. Optional chaining, user.address?.city, is the guard.
Objects are mutable by default. You can add, change, and delete properties after creation. Object.freeze blocks changes, but only one level deep: the frozen object's properties cannot be reassigned, while objects they point to remain editable. There is no built-in deep freeze.
The cost of objects is indirection. Every object is a heap allocation reached through a pointer, reachable objects are never collected, and engines optimise objects whose shape stays consistent. Adding and deleting properties on a hot object, or using delete in a loop, makes the engine fall back to slower storage. Create objects with all their properties up front where you can.
Code Examples
const user = {
id: 7,
email: "ada@example.com",
// A method is a property whose value is a function.
describe() {
return `${this.email} (#${this.id})`;
},
};
// Dot notation for fixed names, brackets for names computed at runtime.
const field = "email";
console.log(user[field]); // => ada@example.com
// Keys are coerced to strings.
const scores = {};
scores[1] = 90;
console.log(Object.keys(scores)); // => [ '1' ]
// Reading a missing property gives undefined, not an error.
console.log(user.age); // => undefined
// Own properties only; inherited ones such as toString are not listed.
console.log(Object.keys(user)); // => [ 'id', 'email', 'describe' ]
console.log(Object.hasOwn(user, "toString")); // => false (ES2022)const defaults = { theme: "light", fontSize: 14 };
const alias = defaults; // same object, not a copy
alias.theme = "dark";
console.log(defaults.theme); // => dark
// Two distinct objects are never ===, however similar they look.
console.log({ a: 1 } === { a: 1 }); // => false
// Spread makes a shallow copy: top level is new, nested values are shared.
const settings = { ...defaults, layout: { columns: 2 } };
const copy = { ...settings };
copy.layout.columns = 3;
console.log(settings.layout.columns); // => 3 (layout was shared)
// freeze is also shallow.
Object.freeze(settings);
settings.theme = "sepia"; // silently ignored outside strict mode
settings.layout.columns = 9; // still allowed
console.log(settings.theme, settings.layout.columns); // => dark 9Common Gotchas
Copies are shallow by default
spread, `Object.assign`, and `Object.freeze` all act on the top level only. Nested objects stay shared or stay mutable. For a genuine deep copy use `structuredClone(value)`, available in Node 17+ and modern browsers, and remember it drops functions and class identity.
A plain object inherits keys you did not write
`"toString" in {}` is `true`, and `for...in` walks inherited enumerable properties. When an object is used as a lookup table keyed by untrusted input, prefer `Map` or `Object.create(null)` so that a key named `constructor` cannot collide with something real.
Property access on `null` or `undefined` throws
`user.address.city` raises `TypeError: Cannot read properties of undefined (reading 'city')`. The failure is one step later than it looks, because `user.address` returned `undefined` quietly first.
Object key order is specified
integer-like keys sort numerically ahead of string keys, regardless of insertion order. If insertion order must be preserved for numeric keys, use a `Map`.