Back to JavaScript Notes
Topic #217

JS Classes

Classes define reusable blueprints with methods, constructors and inheritance. They organise JavaScript's object model clearly.

ECMAScript 2015, also known as ES6, introduced JavaScript Classes.

JavaScript Classes are templates for JavaScript Objects.

JavaScript Class Syntax

Use the keyword class to create a class.

Always add a method named constructor():

Syntax

class ClassName {

   constructor() { ... }

}

Example

class Car {

   constructor(name, year) {

    this.name = name;

    this.year = year;

  }

}

The example above creates a class named "Car".

The class has two initial properties: "name" and "year".

Note: A JavaScript class is not an object. It is a template for JavaScript objects.


Using a Class

When you have a class, you can use the class to create objects:

Example

const myCar1 = new Car("Ford", 2014);

const myCar2 = new Car("Audi", 2019);

The example above uses the Car class to create two Car objects.

Note: The constructor method is called automatically when a new object is created.


The Constructor Method

The constructor method is a special method:

  • It has to have the exact name "constructor"
  • It is executed automatically when a new object is created
  • It is used to initialize object properties

If you do not define a constructor method, JavaScript will add an empty constructor method.


Class Methods

Class methods are created with the same syntax as object methods.

Use the keyword class to create a class.

Always add a constructor() method.

Then add any number of methods.

Syntax

class ClassName {

   constructor() { ... }

  method_1() { ... }

  method_2() { ... }

  method_3() { ... }

}

Create a Class method named "age", that returns the Car age:

Example

class Car {

   constructor(name, year) {

    this.name = name;

    this.year = year;

   }
  age() {

    const date = new Date();

    return date.getFullYear() - this.year;

   }
}

const myCar = new Car("Ford", 2014);

document.getElementById("demo").innerHTML =

"My car is " + myCar.age() + " years old.";

You can send parameters to Class methods:

Example

class Car {
  constructor(name, year) {

  this.name = name;
    this.year = year;
  }

  age(x) {
    return x - this.year;
  }
}

  const date = new Date();

  let year = date.getFullYear();

const myCar = new
  Car("Ford", 2014);
document.getElementById("demo").innerHTML=
"My car is
  " + myCar.age(year) + " years old.";

"use strict"

Classes syntax must be written following The "use strict" Directive.

You will get an error if you do not follow the "strict mode" rules.

Example

class Car {

   constructor(name, year) {

    this.name = name;

    this.year = year;

   }
  age() {

    // date = new Date();   // This will not work

    const date = new Date();  // This will work

    return date.getFullYear() - this.year;

   }
}

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

JS Classes – FAQs

Quick answers about learning JS Classes in JavaScript.

This free note from Coding Now Tech Institute explains JS Classes in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including JS Classes, is 100% free with no signup required.
With focused practice, most students grasp JS Classes in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now