Skip to content

What Is a Class?

In JavaScript Foundations you built objects like this:

const expense = {
id: 1,
description: 'Coffee',
amount: 4.50,
category: 'Food',
date: '2024-01-15',
};

This works fine for one object. But when you need to create dozens of expense objects — each with the same shape, the same default behaviors, the same methods — copying and pasting object literals becomes error-prone and hard to maintain. Classes are the solution.

A class is a blueprint for creating objects. It defines what properties each object has and what methods it can call. Every object you create from a class is called an instance.

class Expense {
constructor(id, description, amount, category, date) {
this.id = id;
this.description = description;
this.amount = amount;
this.category = category;
this.date = date;
}
}
const coffee = new Expense(1, 'Coffee', 4.50, 'Food', '2024-01-15');
const busPass = new Expense(2, 'Bus pass', 30.00, 'Transport', '2024-01-14');
console.log(coffee.description); // 'Coffee'
console.log(busPass.amount); // 30

coffee and busPass are both instances of Expense. They were created from the same blueprint, so they have the same structure. You did not copy the object shape twice — you defined it once.

class ClassName {
constructor(/* arguments */) {
// assign properties using this
}
}
  • The class name follows PascalCase by convention — Expense, User, ShoppingCart.
  • The constructor is a special method that runs when you call new ClassName(...).
  • this inside the class refers to the specific instance being created.

You create an instance with the new keyword:

const gym = new Expense(4, 'Gym membership', 45.00, 'Health', '2024-01-10');

Under the hood, new:

  1. Creates a new empty object
  2. Sets that object’s prototype to Expense.prototype
  3. Calls the constructor with the arguments you passed
  4. Returns the new object

You do not need to understand the prototype mechanism right now. What matters is the pattern: define once, instantiate many times.

Plain object literalClass instance
One-off, no blueprintCreated from a blueprint
Properties set manually each timeProperties set consistently by the constructor
No shared methodsAll instances share the same methods
No type identityexpense instanceof Expense is true

The instanceof operator lets you check whether an object was created from a particular class:

console.log(coffee instanceof Expense); // true
console.log(coffee instanceof Array); // false

Despite the new syntax, JavaScript classes are built on the same prototype-based object system that has always existed. The class keyword is a cleaner syntax for the same thing — it does not introduce a new kind of object. If you ever look at transpiled JavaScript or older code, you will see the equivalent written with constructor functions and .prototype. The class syntax is simpler and more readable.

  1. Define a class called Expense with a constructor that accepts id, description, amount, category, and date.
  2. Create five instances using the BudgetBuddy expense data from Module 01.
  3. Log expense.description and expense.amount for each instance to confirm the properties are set correctly.
  4. Use instanceof to check that each instance is an Expense.
  • A class is a blueprint for creating objects — define it once, instantiate it many times.
  • The constructor method runs when you call new ClassName(...) and sets up properties via this.
  • Class names use PascalCase.
  • Instances share structure and — as you will see next — methods.
  • JavaScript classes are syntax over the existing prototype system, not a new data type.