Skip to content

Object

An object is an instance of a class. It’s a concrete entity that exists in memory, with its own unique set of data (property values) and the ability to perform actions (methods).

If a class is a blueprint, an object is the actual thing built from that blueprint.

  • Objects represent real-world entities in code
  • They allow multiple independent copies of the same structure
  • They encapsulate state (data) and behavior (methods) together
  • To work with concrete instances of data
  • Each object has its own identity and state
  • Objects can interact with each other through messages (method calls)

ConceptAnalogy
Class (Car)The design blueprint at the factory
Object (My Car)The actual car you drive — it has a VIN, color, and mileage
PropertiesYour car’s color = “Red”, mileage = 15,000
MethodsYour car can start(), stop(), accelerate()
IdentityEach car has a unique license plate

Two cars built from the same blueprint are different objects:

  • MyCar: Red Toyota Camry, license ABC-123
  • YourCar: Blue Toyota Camry, license XYZ-789

An object is a self-contained entity that contains:

  • State (properties/fields) — Data about the object
  • Behavior (methods) — Actions the object can perform
  • Identity — A unique reference that distinguishes it from other objects

Without objects, you work with disconnected data and functions:

// Disconnected data and functions
const userName = "John";
const userEmail = "john@email.com";
const userAge = 30;
function sendEmail(name, email) { /* ... */ }
function celebrateBirthday(name, age) { /* ... */ }

With objects, related data and behavior are bundled together:

class User {
constructor(name, email, age) {
this.name = name;
this.email = email;
this.age = age;
}
sendEmail() { /* Uses this.name, this.email */ }
celebrateBirthday() {
this.age++;
console.log(`Happy birthday ${this.name}!`);
}
}
const user = new User("John", "john@email.com", 30);
user.celebrateBirthday(); // "Happy birthday John!"

graph TD
Class[Car Class<br/>Blueprint] --> Object1[Object: MyCar<br/>Red Toyota, ABC-123]
Class --> Object2[Object: YourCar<br/>Blue Honda, XYZ-789]
Class --> Object3[Object: TheirCar<br/>Black Ford, DEF-456]
subgraph Object1_Details["MyCar Object Details"]
P1[brand: 'Toyota'<br/>color: 'Red'<br/>year: 2023]
M1[start() stop()<br/>accelerate()]
end
Object1 --> Object1_Details
graph LR
subgraph Stack["Stack Memory"]
Ref1[car1 → 0xFFA1]
Ref2[car2 → 0xFFB2]
end
subgraph Heap["Heap Memory"]
Obj1[Object at 0xFFA1<br/>brand: 'Toyota'<br/>color: 'Red'<br/>methods: ref to class]
Obj2[Object at 0xFFB2<br/>brand: 'Honda'<br/>color: 'Blue'<br/>methods: ref to class]
end
Ref1 --> Obj1
Ref2 --> Obj2

// Creating objects from a class
class Smartphone {
constructor(brand, model, price) {
this.brand = brand;
this.model = model;
this.price = price;
this.batteryLevel = 100;
}
charge(amount) {
this.batteryLevel = Math.min(100, this.batteryLevel + amount);
}
use(amount) {
this.batteryLevel = Math.max(0, this.batteryLevel - amount);
}
getInfo() {
return `${this.brand} ${this.model} — $${this.price}`;
}
}
// Creating objects (instances)
const phone1 = new Smartphone("Apple", "iPhone 15", 999);
const phone2 = new Smartphone("Samsung", "Galaxy S24", 899);
// Objects have independent state
phone1.use(30);
console.log(phone1.batteryLevel); // 70
console.log(phone2.batteryLevel); // 100
console.log(phone1.getInfo()); // "Apple iPhone 15 — $999"
CharacteristicDescriptionExample
IdentityEach object has a unique referenceobj1 !== obj2 even with same values
StateCurrent values of all propertiesuser.name = "John", user.age = 30
BehaviorWhat the object can douser.greet(), user.login()
EncapsulationInternal details are hiddenPrivate fields, public methods
PolymorphismDifferent objects can respond to same messageanimal.speak()

const obj = new ClassName(args);
const obj = { key: "value", method() { /* ... */ } };
function createUser(name) {
return { name, greet() { return `Hi ${this.name}`; } };
}
const proto = { greet() { return "Hello"; } };
const obj = Object.create(proto);

  • Initialize all properties in the constructor
  • Use meaningful variable names for objects
  • Don’t mutate object properties from outside when possible
  • Use getters/setters for controlled access
  • Treat objects as the smallest unit of reusable code

  • Comparing objects with == — Compare references, not values
  • Mutating shared state — Objects passed by reference can be modified unexpectedly
  • Too many objects — If you have thousands of objects, consider data-oriented design
  • Shallow copies — Object.assign and spread only copy one level deep

Easy:

  1. What is an object in OOP?
  2. What’s the difference between a class and an object?
  3. How do you create an object from a class?

Medium: 4. How are objects stored in memory? 5. What is object identity and why does it matter? 6. Explain pass-by-reference vs pass-by-value for objects.

Advanced: 7. How does JavaScript’s prototypal inheritance differ from classical inheritance for objects? 8. What is the object lifecycle from creation to garbage collection? 9. Explain how object pooling works and when it’s useful.


ConceptKey Point
ObjectAn instance of a class — a concrete entity with state and behavior
IdentityEach object is unique, even with same values
StateThe property values of an object at a given time
BehaviorMethods that define what the object can do
Creationnew ClassName(args) creates an object in memory
MemoryObjects live on the heap, referenced by variables on the stack

Previous Topic: Class → Next Topic: Constructor → Related Topics: Class, Object Lifecycle