top of page

Classes in JavaScript

In JavaScript we can use the keyword class to create a class, which is another way of creating an object. The class it self is not an object, but are a template or blueprint if you like, to create an object.

​

The syntax is similar to how it's done in other programming languages and is very simple. let's have a look at an example: 

The class is created by using the keywords class and constructor. The constructor is used to initialize the class. You always have to add a method named constructor when creating a class. If you do not define a constructor method, JavaScript will add an empty constructor method.

​

Let's have a look at an example:

The function takes values in through the parameters and assign the values to the properties in the class using the keyword this.

​

With the class we can now create objects with it. Let's use the Car class and create some car objects.

We can add methods to the class just like with objects.

​

The syntax for this is a following:

The implementation of methods in a class can look like this:

bottom of page