JavaScript Class Tutorial: Build a Simple Example
Learn how JavaScript classes work with a clear, simple example. See how constructors and methods come together in real code.


What Is a JavaScript Class and How Is It Used?
In JavaScript, classes are a structure that defines an object’s properties and behavior. This structure is used to create an instance of an object from a class. For example, you can create a car class, and every car object you create from that class will use the properties and behavior of the car class.
To use JavaScript classes, you use the “class” keyword. You define the class by giving it a name. Then, inside the class, you create a constructor function. This function is called when a new instance of the class is created and sets the instance’s properties.
Below is a simple example of a car class:
class Araba {
constructor(marka, model, renk) {
this.marka = marka;
this.model = model;
this.renk = renk;
}
hareketEttir() {
console.log(`${this.marka} ${this.model} harekete geçti!`);
}
}
const araba1 = new Araba("Toyota", "Corolla", "siyah");
const araba2 = new Araba("Ford", "Mustang", "kırmızı");
araba1.hareketEttir(); // Toyota Corolla started moving!
araba2.hareketEttir(); // Ford Mustang started moving!
In this example, the Araba class is created and the brand, model, and color properties are added with the constructor function. A function named hareketEttir() is added to the Araba class, and finally, two car objects named araba1 and araba2 are created and we called the hareketEttir() function. However,
Classes can also have other features, for example:
- Properties: A class’s properties are the properties that will be used when a new instance of the class is created. For example, the brand, model, and color properties of the car class.
- Methods: A class’s methods define the behavior that can be used when a new instance of the class is created. For example, the hareketEttir() method of the car class.
- Instance variables: These are the variables assigned to the properties of the object created when a new instance of the class is created. For example, the brand, model, and color properties of the araba1 object.
- Instance methods: These define the behavior that the object created when a new instance of the class is created can use. For example, the hareketEttir() method of the araba1 object.
- Static properties and methods: These are properties and methods that can be used in common by all instances of the class. For example, a common speed limit that all car objects can use.
To understand classes better, let’s examine the following example together:
class Araba {
// Static property
static hizSiniri = 120;
// Properties
marka;
model;
renk;
hiz = 0;
// Constructor function
constructor(marka, model, renk) {
this.marka = marka;
this.model = model;
this.renk = renk;
}
// Methods
hareketEttir() {
console.log(`${this.marka} ${this.model} harekete geçti!`);
}
hiziArttir(artisMiktari) {
this.hiz += artisMiktari;
if (this.hiz > Araba.hizSiniri) {
this.hiz = Araba.hizSiniri;
console.log("Hız sınırına ulaşıldı!");
}
}
hiziAzalt(azalisMiktari) {
this.hiz -= azalisMiktari;
if (this.hiz < 0) {
this.hiz = 0;
console.log("Araba durdu.");
}
}
}
const araba1 = new Araba("Toyota", "Corolla", "siyah");
const araba2 = new Araba("Ford", "Mustang", "kırmızı");
araba1.hareketEttir(); // Toyota Corolla started moving!
araba2.hiziArttir(100); // Ford Mustang's speed was increased by 100.
araba2.hiziArttir(50); // Speed limit reached!
araba2.hiziAzalt(200); // The car stopped.
A speed property and the hiziArttir() and hiziAzalt() methods have been added to the Araba class. In addition, the static property hizSiniri, which all car objects can use in common, has also been defined. Finally, two car objects named araba1 and araba2 have been created, and the hareketEttir(), hiziArttir(), and hiziAzalt() methods have been called.
Building on this class example, let’s also look at the question of what a constructor is together in the next article.
Share it if it helped


