Object-Oriented Programming in JavaScript
JavaScript·3 min read·Jan 1, 2025
In JavaScript, object-oriented programming (OOP) is a programming paradigm centered around the concept of classes, which serve as blueprints for creating objects.
Classes are used to define the structure and behavior of the objects created from them called instances, specifying fields (properties) to hold data and methods (functions) to operate on that data.
Overall, they allow developers to create reusable, modular code that models real-world entities (e.g., a user, an animal, etc) and their relationships.
Declaring a new class
To declare a new class, you can use the class keyword as follows:
class ClassName { // class body}Where ClassName is the user-defined name of the class.
Note: By convention, class names always start with a capital letter.
Instantiating a class
To create an instance of a class (an object), you can use the new keyword, followed by the name of the class, and a pair of parentheses ():
let object = new ClassName();Class fields
A class field is a property defined within a class, that allows you to define and initialize properties for objects created from that class.
Unlock the Build CLI Apps in JavaScript & Node.js module
Learn how to build, integrate, and safeguard scalable CLI tools, scripts, and apps with JavaScript, Node.js, npm, and Git.
You get immediate access to:
- 45 focused lessons across JavaScript, Node.js, npm, and Git
- 17 real-world projects with commented solutions
- Ongoing updates to this bundle
- Lifetime access to this bundle