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:
ClassNameis 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:
- Define a
Bookclass.- Define a
titleclass field and initialize it with the string"1984". - Define a
authorclass field and initialize it with the string"George Orwell".
- Define a
- Declare a
bookvariable and initialize it with an instance of theBookclass. - Output the properties of the
bookobject.
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:
-
Define a
Calculatorclass.- Define a
addclass method that returns the sum of two numbers. - Define a
subtractclass method that returns the subtraction of two numbers.
- Define a
-
Declare a
calcvariable and initialize it with an instance of theCalculatorclass. -
Output the return value of the execution of the
add()method. -
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