R
Rishtaara
JavaScript Fundamentals
Lesson 18 of 28Article16 min

Classes

The class keyword is modern sugar over prototypes. It groups constructor logic and methods in one clear block.

Class syntax

The class keyword is modern sugar over prototypes. It groups constructor logic and methods in one clear block.

Real-life example: A class is like a school admission form template — every student fills the same fields, but each student has their own name and roll number.

Basic class
class Lesson {
  constructor(title, duration) {
    this.title = title;
    this.duration = duration;
    this.completed = false;
  }

  complete() {
    this.completed = true;
    return `${this.title} done!`;
  }
}

const intro = new Lesson("JS Intro", "10 min");
intro.complete(); // JS Intro done!

Constructor

The constructor runs when you use new ClassName(). Use it to set up starting values.

Real-life example: When a new phone is turned on for the first time, setup runs once — language, time, account. Constructor is that first-time setup.

Constructor with defaults
class QuizQuestion {
  constructor(text, options, answerIndex = 0) {
    this.text = text;
    this.options = options;
    this.answerIndex = answerIndex;
  }
}

const q1 = new QuizQuestion(
  "What runs in the browser?",
  ["Python", "JavaScript", "C++"],
  1
);

Methods

Methods are functions inside a class. They describe what objects of that class can do.

Real-life example: A car class has methods like startEngine, brake, and honk — actions the car can perform.

Instance methods
class ShoppingCart {
  constructor() {
    this.items = [];
  }

  add(item) {
    this.items.push(item);
  }

  total() {
    return this.items.reduce((sum, i) => sum + i.price, 0);
  }
}

const cart = new ShoppingCart();
cart.add({ name: "Notebook", price: 120 });
cart.add({ name: "Pen", price: 20 });
console.log(cart.total()); // 140

extends and super

extends creates a child class from a parent class. super calls the parent constructor or parent methods.

Real-life example: A ElectricCar extends Car — it has everything a normal car has, plus a battery. super is like saying "do what my parent does first, then add my extra step."

Inheritance
class Animal {
  constructor(name) {
    this.name = name;
  }
  speak() {
    return `${this.name} makes a sound`;
  }
}

class Dog extends Animal {
  constructor(name, breed) {
    super(name); // call Animal constructor
    this.breed = breed;
  }
  speak() {
    return `${super.speak()} — actually, woof!`;
  }
}

new Dog("Bruno", "Lab").speak();

Static methods (brief)

Static methods belong to the class itself, not to each instance. Use them for helpers that do not need object data.

Real-life example: A school office phone number (static) is the same for every student. You call the school, not each student, for the address.

Static helper
class MathHelper {
  static add(a, b) {
    return a + b;
  }
  static isEven(n) {
    return n % 2 === 0;
  }
}

MathHelper.add(3, 4);     // 7 — no "new" needed
MathHelper.isEven(10);    // true