Object literal
The most common way to create an object: { key: value }.
Objects group related data under named properties. They are the foundation for structured JavaScript state.
const profile = {
name: "User A",
role: "editor",
active: true
};
console.log(profile.name);
Objects, Prototypes & Classes
Objects are used when values belong together. Instead of separate variables for name, role and status, one object can describe one profile, setting, response or UI state.
Object properties are usually read with dot notation. Bracket notation is useful when the property name is stored in a variable or contains characters that dot notation cannot use.
Objects are reference values. Assigning an object to another variable does not copy the whole object. It copies a reference to the same object.
The most common way to create an object: { key: value }.
A named value stored on the object.
profile.name reads a simple property name.
Two variables can point to the same object.
Examples
const panelState = {
title: "Dashboard",
open: true,
selectedTab: "overview"
};
const title = "Dashboard"; const open = true; const selectedTab = "overview"; // These values belong together, but the structure does not show it.
Code patterns
These patterns focus on the pieces developers actually reuse: object literals, methods, constructors, prototypes, getters, copying, classes and inheritance.
Group related values under clear property names.
const profile = {
name: "User A",
role: "editor"
};
Use dot notation for normal property names.
const status = { label: "Ready" };
console.log(status.label);
Use brackets when the key is dynamic.
const key = "label";
const status = { label: "Ready" };
console.log(status[key]);
Create a shallow copy before changing state.
const original = { active: false };
const updated = { ...original, active: true };
console.log(updated);
Rules that matter
Objects are where JavaScript programs start to model real application state. Good object shape makes later functions, classes and UI updates much easier to maintain.
If values describe one thing, group them.
A property name should explain the value.
Dot notation cannot use variables as property names.
Changing one reference changes the same underlying object.
Spread can create a shallow copy for simple state changes.
Split objects when one structure starts doing too many jobs.
Production thinking
Objects let code model real data instead of passing loose values around. That makes functions easier to call and state easier to inspect.
UI state objects can keep labels, expanded states and selected values together, which helps accessible rendering stay consistent.
Production code should treat shared objects carefully, especially when passing state between modules or components.
Objects often hold page metadata, link data and structured content. Clear object shape makes rendered output easier to trust.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Object-oriented JavaScript becomes much easier when you can explain where the data lives, where the method is found and which object receives this.