Object
An object is a collection of key-value pairs (properties) that represent a real-world entity or concept. In JavaScript, objects can hold any values as properties — including functions (methods) — and are the most fundamental data structure.
Explanation
In JavaScript, almost everything is an object (or behaves like one). Arrays are objects with numeric keys. Functions are callable objects. null is the only object-like value that isn't actually an object. Plain objects ({}) are the most basic: a collection of named properties you can read, set, and delete. Object literals ({name: 'Alice', age: 30}) create objects inline. The Object constructor (new Object()), Object.create(), and class constructors are other ways to create objects. Object.assign() and the spread operator ({...obj}) create shallow copies. Object.freeze() makes an object immutable. Object.keys(), Object.values(), and Object.entries() iterate over enumerable properties. Prototype chain: every object has an internal [[Prototype]] link to another object (its prototype). When you access a property, JavaScript looks on the object first, then up the prototype chain until it finds the property or reaches null. This is how methods "inherited" from a class work — they live on the class's prototype, not on each instance. Destructuring is the modern way to extract object properties: const {name, age} = user instead of const name = user.name; const age = user.age. It supports defaults ({name = 'Anonymous'} = user), renaming ({name: userName} = user), and nested destructuring. These patterns appear constantly in modern JavaScript and are often generated by AI without explanation.
Code Example
javascript// Object fundamentals in JavaScript
// Literal creation
const person = {
firstName: 'Alice',
lastName: 'Smith',
age: 30,
greet() { return `Hi, I'm ${this.firstName}`; }
};
// Property access: dot vs bracket
console.log(person.firstName); // 'Alice'
console.log(person['lastName']); // 'Smith' (use bracket for dynamic keys)
const key = 'age';
console.log(person[key]); // 30
// Destructuring
const { firstName, age, role = 'user' } = person; // role defaults to 'user'
const { firstName: name } = person; // rename to 'name'
// Spread: shallow copy / merge
const updated = { ...person, age: 31 }; // new object with age overridden
const merged = { ...defaults, ...userSettings }; // merge, userSettings wins
// Optional chaining: safe nested access
const city = user?.address?.city; // undefined instead of TypeError
// Object.entries for iteration
for (const [key, value] of Object.entries(person)) {
console.log(`${key}: ${value}`);
}
// Computed property names
const propName = 'email';
const contact = { [propName]: 'alice@example.com' }; // {email: '...'}
Why It Matters for Engineers
Objects are the fundamental building block of JavaScript programs. Understanding the prototype chain explains how method inheritance works, why hasOwnProperty() exists, and what's happening when AI-generated code uses Object.create(). Understanding shallow vs deep copies (spread only copies one level deep) prevents mutation bugs in state management. Optional chaining (?.) and nullish coalescing (??) — both relatively new JavaScript features — prevent enormous amounts of "Cannot read property of undefined" errors. Knowing these patterns lets you write defensive code and understand the AI-generated code that uses them.
Related Terms
Class · Prototype · Hash Table · Immutability
Learn This In Practice
Go deeper with the full module on Beyond Vibe Code.
Programming Foundations → →