Skip to main content
Web Development

Classes in JavaScript

Ravi Kant Ranjan Ravi Kant Ranjan, Principal Developer - SAP Fiori, BTP 3 min read

In JavaScript, ‘class’ is a kind of function, but instead of using the keyword function to initialize it, we use the keyword ‘class,’ and all the properties are assigned inside a constructor() method of a class.

A class is a user-defined blueprint or prototype consisting of the same objects. Each object of a class will share a standard set of properties or methods. ES6 provides a keyword “class” to create classes in JavaScript. Classes in JavaScript were introduced in ECMAScript 2015.

Let’s understand it by examples:

JavaScript code: Person constructor function setting name, age and gender, creating P1 and P2 with new and logging them

The above code has a parameterized function called “Person.” And we have two objects, P1 and P2, of type “Person.” Though JavaScript is an object-based language, everything in JavaScript is an object. This is also one of the old ways to create objects with a new keyword. Usually, in other programming languages, we create objects with new keywords while using the concepts of classes. But in JavaScript, we can create objects with a function as well. Below is the output of the above code.

Browser console output showing two Person objects: John, 25, Male and Doe, 30, Male

Now let’s see another example with a class in JavaScript. In JavaScript class is written using the class keyword as in other languages.

Syntax:- class ClassName { }

JavaScript code: class Student with method fun1 that logs "fun1 called !!", then obj = new Student() and obj.fun1()

Here fun1 is a function in class Student, and obj is its object. To access the function inside a class, we write ObjectName.functionName(). 

Here className.functionName() = obj.fun1(). Below is the output of the above code.

Chrome DevTools console showing the output "fun1 called !!"

Another example:

JavaScript code: class Student with fun1, creating obj and obj2 and calling fun1 on each

Here there are two objects, obj, and obj2, which call the same function, fun1(). So the output is two times “fun1 called !!”

Chrome DevTools console showing "fun1 called !!" printed twice

Another example with a constructor:

JavaScript code: class Student with a constructor logging "constructor called !!" and a fun1 method, then new Student()

Constructor in JavaScript is a special kind of function without any name. It’s written as a constructor() {}. Constructor is invoked whenever an object is created, as in the above example.

Chrome DevTools console showing the output "constructor called !!"

Example with constructor and function:

JavaScript code: class Student with constructor and fun1, two objects created and fun1 called on each

Chrome DevTools console showing "constructor called !!" and "fun1 called !!" each printed twice

To access variables of a constructor anywhere in the class, the “this” keyword is used.

JavaScript code: constructor sets this.studentName, this.age and this.emailId, and fun1 logs them in a template string

Console output: "Student name is John and age is 25 and email is john@gmail.com" printed twice

Parameterized constructor in JavaScript:

JavaScript code: constructor(name, age, email) assigns this properties, objects created for John and Doe with arguments

Console output: student name, age and email lines for John (25) and Doe (20)

In the above example, we passed parameters to the constructor by passing arguments while creating objects. These arguments go and are set as the parameter in the constructor.

Hence we now have a good understanding of classes in JavaScript, How to define a class, What a constructor and parameterized construction are, and How to access the class and the members of the class.

These topics are usually left untouched while learning JavaScript but are really important from a development point of view. 

Happy Learning!

If you have an interest in viewing similar content, visit our blog, here. 

View our LinkedIn, here. 

Share