Defining Classes
in JavaScript
Learn how to create blueprints for objects using the class syntax: constructors, methods, and instances that bring your code to life.
What Is a Class?
A class is a blueprint or template that describes the properties (data) and methods (behavior) an object will have. Instead of writing the same object structure over and over, you define a class once and create as many objects from it as you need.
Key Vocabulary
Why Do Classes Matter?
Without classes, you must manually build every object with the same repeated structure. Classes solve three critical problems:
Anatomy of a Class
The Constructor Method
The constructor is a special method that runs automatically when you create a new instance with the new keyword. Its job is to set initial property values using this.
constructor(width, height) {
this.width = width;
this.height = height;
}
}
Classes vs Object Literals
| Class | Object Literal | |
|---|---|---|
| Syntax | class Dog { ... } | { name: "Rex" } |
| Reusable? | Yes: create many instances | No: one-off object |
| Constructor | Built-in constructor method | None |
| Methods | Shared on prototype | Copied per object |
| Best for | Multiple similar objects | Single configuration/data |
How to Define a Class
Example 1: Student Class
constructor(name, grade) {
this.name = name;
this.grade = grade;
}
introduce() {
return `Hi, I'm ${this.name} in grade ${this.grade}.`;
}
}
const alice = new Student("Alice", 10);
alice.introduce(); // "Hi, I'm Alice in grade 10."
Example 2: BankAccount
constructor(owner, balance) {
this.owner = owner;
this.balance = balance;
}
deposit(amount) {
this.balance += amount;
return this.balance;
}
withdraw(amount) {
if (amount > this.balance) {
return "Insufficient funds";
}
this.balance -= amount;
return this.balance;
}
}
Example 3: TaskList with Arrays
constructor(title) {
this.title = title;
this.tasks = []; // initialize empty array
}
addTask(task) {
this.tasks.push(task);
}
getCount() {
return this.tasks.length;
}
summarize() {
return `${this.title}: ${this.getCount()} tasks`;
}
}
From Class to Instances
Understanding this
Inside a class, this always refers to the specific instance that called the method. It is the bridge between the class blueprint and the actual object.
constructor(name) {
this.name = name; // "this" = the new Cat
}
greet() {
return `Meow, I'm ${this.name}`;
}
}
const whiskers = new Cat("Whiskers");
whiskers.greet(); // "this" = whiskers
// "Meow, I'm Whiskers"
Methods Working Together
constructor() {
this.count = 0;
}
increment() {
this.count++;
}
decrement() {
this.count--;
}
getStatus() {
return `Count is ${this.count}`;
}
}
const c = new Counter();
c.increment();
c.increment();
c.getStatus(); // "Count is 2"
Common Mistakes
Class Syntax Summary
| Concept | Syntax | Purpose |
|---|---|---|
| Declare a class | class Name { } | Create a reusable blueprint |
| Constructor | constructor(params) | Initialize instance properties |
| Set a property | this.prop = value | Attach data to the instance |
| Define a method | methodName() { } | Add behavior to instances |
| Create an instance | new Name(args) | Build an object from the class |
| Call a method | instance.method() | Execute behavior on an instance |
Question 1 of 5
Which keyword is used inside a class to refer to the current instance?
Question 2 of 5
What happens if you call a class without the new keyword?
Question 3 of 5
class Car {
constructor(make, year) {
this.make = make;
this.year = year;
}
age() {
return 2025 - this.year;
}
}
const myCar = new Car("Toyota", 2020);
What does myCar.age() return?
Question 4 of 5
How are methods separated inside a class body?
Question 5 of 5
class Playlist {
constructor(name) {
this.name = name;
this.songs = [];
}
addSong(song) {
this.songs.push(song);
}
count() {
return this.songs.length;
}
}
const rock = new Playlist("Rock Hits");
rock.addSong("Bohemian Rhapsody");
rock.addSong("Stairway to Heaven");
What does rock.count() return after the code above runs?