JavaScript classes provide a structured, modern syntax to create objects and manage inheritance. They are essentially syntactic sugar over JavaScript’s prototype-based system, but they make object-oriented code easier to read, write, and maintain.
With classes you can:
Person, Animal, User).extends and super().new; used to initialize properties.const p = new Person()).extends.#name, only accessible inside the class.A basic class uses the class keyword followed by a name (by convention, starting with a capital letter). Inside, you typically define a constructor and some methods.
Even though it looks similar to classes in other languages (like Java or C#), JavaScript classes still use prototypes under the hood. The class syntax just makes it cleaner and less error-prone.
Here’s a simple Person class with a constructor and a method:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hello, my name is ${this.name}.`);
}
}
const person1 = new Person("Alice", 30);
person1.greet(); // Hello, my name is Alice.
new Person("Alice", 30) calls the constructor and creates an object with name and age.greet() is defined once on the prototype and shared by all Person instances.person1.greet() prints: Hello, my name is Alice. in the console.Classes can inherit from other classes using the extends keyword. The child class can override or add new methods. Use super() to call the parent constructor or methods.
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a sound.`);
}
}
class Dog extends Animal {
speak() {
console.log(`${this.name} barks.`);
}
}
const dog = new Dog("Buddy");
dog.speak(); // Buddy barks.
Dog extends Animal means Dog inherits from Animal.Dog class overrides speak() to provide a more specific behavior.dog.speak() calls the overridden method, so you get Buddy barks. in the console.Getters and setters let you run logic whenever a property is read or updated. It’s common to use an underscore prefix (like _name) for the internal property.
class User {
constructor(name) {
this._name = name;
}
get name() {
return this._name.toUpperCase();
}
set name(newName) {
if (newName.length > 0) {
this._name = newName;
}
}
}
const user = new User("bob");
console.log(user.name); // BOB
user.name = "sam";
console.log(user.name); // SAM
get name() method is called when you read user.name.set name() method runs when you assign user.name = "sam".Private fields start with # and are only accessible inside the class. They are great for hiding internal details.
class BankAccount {
#balance = 0;
deposit(amount) {
this.#balance += amount;
}
getBalance() {
return this.#balance;
}
}
const account = new BankAccount();
account.deposit(100);
console.log(account.getBalance()); // 100
// console.log(account.#balance); ❌ SyntaxError
#balance cannot be accessed from outside the class; trying to do so is a syntax error.deposit, getBalance) can use #balance.Static methods belong to the class itself, not to instances. They are often used for helper or utility functions.
class MathUtils {
static square(x) {
return x * x;
}
}
console.log(MathUtils.square(4)); // 16
MathUtils.square(4), not on an instance.User, Product, Order, Car.Player, Enemy, Weapon).Click the button to create a Person instance and see the greeting message below.
Click the button to simulate person.greet() output.
BankAccount).Product class with a name and price and a method to display details.Book class that extends Product and adds an author property.Product to compare prices of two products.