Define & Instantiate Classes in JavaScript

6 min read·Jan 1, 2025

In JavaScript, a class is a reusable template for creating standardized objects.

Classes are used to describe the internal data structure of these objects (their properties) and implement their functionalities (their methods).

In this lesson, you will learn how to declare classes, define their properties and methods, and create objects from them.

Define a class

In JavaScript, classes are defined using the class keyword:

class ClassName {
  // class body
}

Where:

  • ClassName is the user-defined name of the class.
  • {} is the body of the class that contains its fields and methods.

Note: By convention, class names always start with a capital letter and are written in CamelCase.

Instantiate a class

To instantiate a class, which means creating a object from it (i.e. an instance), you can use the new keyword, followed by the name of the class, and a pair of parentheses ():

let object = new ClassName();

Define class fields

A class field is an element of a class that allows you to define and initialize a property of the resulting instance.

Class fields are declared without the use of the let or const keywords, and are assigned an undefined value by default.

class ClassName {
  fieldA;
  fieldB = value;
}

Example

Let's consider this script:

class Book {
  title = '1984';
  author = 'George Orwell';
}

const book = new Book();

console.log(book.title + ' written by ' + book.author);

When executed, it will:

  1. Define a Book class.
    1. Define a title class field and initialize it with the string "1984".
    2. Define a author class field and initialize it with the string "George Orwell".
  2. Declare a book variable and initialize it with an instance of the Book class.
  3. Output the properties of the book object.

Which will produce this output:

1984 written by George Orwell

Define class methods

A class method is an element of a class that allows you to define and implement a method of the resulting instance.

Class methods are declared without the use of the function keyword.

class ClassName {
  method(param1, ..., paramN) {
    //
  }
}

Example

Let's consider this script:

class Calculator {
  add(a, b) {
    return a + b;
  }

  subtract(a, b) {
    return a - b;
  }
}

let calc = new Calculator();

console.log('1 + 2 = ' + calc.add(1, 2));
console.log('10 - 6 = ' + calc.subtract(10, 6));

When executed, it will:

  1. Define a Calculator class.

    1. Define a add class method that returns the sum of two numbers.
    2. Define a subtract class method that returns the subtraction of two numbers.
  2. Declare a calc variable and initialize it with an instance of the Calculator class.

  3. Output the return value of the execution of the add() method.

  4. Output the return value of the execution of the subtract() method.

Which will produce this output:

1 + 2 = 3
10 - 6 = 4

Enjoying the courses?

I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:

Support BackendBrewery
Define & Instantiate Classes in JavaScript | Backend Brewery