JavaScript Classes

A Blueprint for Objects

JavaScript Classes

Classes give you a clean syntax for creating many similar objects. A constructor runs on each new instance; methods are shared.

5 min read Level 2/5 #classes#constructor#methods
What you'll learn
  • Define a class with a constructor and methods
  • Create instances with `new`
  • Understand that methods are shared via the prototype

A class is a blueprint for creating objects of a certain shape. The constructor runs each time you create an instance; methods on the class are shared across all instances.

A class script.js
class User {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  greet() {
    return `Hi, I'm ${this.name}`;
  }
}

const ada = new User("Ada", 36);
const lin = new User("Lin", 28);

console.log(ada.greet()); // "Hi, I'm Ada"
console.log(lin.greet()); // "Hi, I'm Lin"
▶ Preview: console

Key things:

  • class User { … } declares a class.
  • constructor runs when you new an instance.
  • this inside the constructor and methods is the instance.
  • new User(…) creates a new instance, calling the constructor.

Methods Are Shared

All instances share the same method functions — they’re stored on the class’s prototype (covered in the previous lesson).

Methods are shared script.js
class User {
  constructor(name) { this.name = name; }
  greet() { return `Hi, ${this.name}`; }
}

const a = new User("Ada");
const b = new User("Lin");

console.log(a.greet === b.greet); // true — same function reference
▶ Preview: console

This saves memory and means an update to a method affects all existing instances.

Instance Properties

The simplest form sets properties inside the constructor with this.x = ….

You can also declare instance properties at the top of the class (ES2022) — they’re set on every new instance before the constructor runs:

Class fields script.js
class Counter {
  count = 0;        // default for every new Counter

  increment() {
    this.count++;
  }
}

const c = new Counter();
c.increment();
c.increment();
console.log(c.count); // 2
▶ Preview: console

class vs Plain Object

If you only need one of something, a plain object is enough:

const config = {
  apiUrl: "https://example.com",
  timeout: 5000,
};

If you’ll make many similar instances with their own state, a class is the natural fit.

Forgetting new

Calling a class without new throws.

new is required script.js
class User {
  constructor(name) { this.name = name; }
}

// User("Ada"); // TypeError: Class constructor User cannot be invoked without 'new'
const ada = new User("Ada"); // ✅
console.log(ada);
▶ Preview: console

Methods Use Regular Function Syntax

Class methods are regular methods, not arrows. this inside a method follows the normal “method call” rule.

Try It Yourself

Exercise

A BankAccount class

Difficulty 2/5~4 min
Write a class `BankAccount` with: - A `constructor(initial)` that sets `this.balance = initial`. - A method `deposit(amount)` that adds to the balance. - A method `withdraw(amount)` that subtracts from the balance. - A method `getBalance()` that returns the current balance. Then create an account with `100`, deposit `50`, withdraw `30`, and log the balance. Expected: `120`.
solution.js
// declare BankAccount here

const acct = new BankAccount(100);
acct.deposit(50);
acct.withdraw(30);
console.log(acct.getBalance());
3tests will run
💡 Show hint
Use the modern class syntax. The methods all live inside the `class { … }` block, no `this.method = function () { … }` needed.
✅ Show solution
class BankAccount {
  constructor(initial) {
    this.balance = initial;
  }
  deposit(amount)  { this.balance += amount; }
  withdraw(amount) { this.balance -= amount; }
  getBalance()     { return this.balance; }
}

const acct = new BankAccount(100);
acct.deposit(50);
acct.withdraw(30);
console.log(acct.getBalance());

Up Next

Build one class on top of another with extends.

JavaScript Class Inheritance →