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.
What a class is
Section titled “What a class is”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); // 30coffee 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.
The class syntax
Section titled “The class syntax”class ClassName { constructor(/* arguments */) { // assign properties using this }}- The class name follows
PascalCaseby convention —Expense,User,ShoppingCart. - The
constructoris a special method that runs when you callnew ClassName(...). thisinside the class refers to the specific instance being created.
Creating instances with new
Section titled “Creating instances with new”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:
- Creates a new empty object
- Sets that object’s prototype to
Expense.prototype - Calls the
constructorwith the arguments you passed - 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.
Classes vs plain objects
Section titled “Classes vs plain objects”| Plain object literal | Class instance |
|---|---|
| One-off, no blueprint | Created from a blueprint |
| Properties set manually each time | Properties set consistently by the constructor |
| No shared methods | All instances share the same methods |
| No type identity | expense instanceof Expense is true |
The instanceof operator lets you check whether an object was created from a particular class:
console.log(coffee instanceof Expense); // trueconsole.log(coffee instanceof Array); // falseClasses are not a new data type
Section titled “Classes are not a new data type”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.
Exercise
Section titled “Exercise”- Define a class called
Expensewith a constructor that acceptsid,description,amount,category, anddate. - Create five instances using the BudgetBuddy expense data from Module 01.
- Log
expense.descriptionandexpense.amountfor each instance to confirm the properties are set correctly. - Use
instanceofto check that each instance is anExpense.
- A class is a blueprint for creating objects — define it once, instantiate it many times.
- The
constructormethod runs when you callnew ClassName(...)and sets up properties viathis. - 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.