
For a long time, developers used constructor functions and prototypes to create objects. ES6 introduced classes — a cleaner, more intuitive syntax for creating and working with objects. This article explains what JavaScript classes are, how to use them, and includes practical examples.
Most of us have used the constructor functions to create an object of specific type. The constructor function is simply a function, but when used with the new and this keyword, it becomes a constructor function:
function Person(first, last, age) { this.firstName = first; this.lastName = last; this.age = age; } const person1 = new Person("Wael", "Salah", 33); const person2 = new Person("John", "Doe", 40);
In ES6 this example can be rewritten using a class (the new syntax).
🔹 What is a JavaScript Class?
A class in JavaScript is essentially a template for creating objects. It encapsulates data (properties) and behavior (methods). While classes use a new syntax, they’re still based on JavaScript’s prototype system under the hood.
🔹 Basic Syntax