How to Use Modules and Classes in Object-Oriented JavaScript?

Estimated read time 1 min read

Modules and classes are key concepts in object-oriented JavaScript. Here are the steps to use them:

  1. Define a module: A module is a self-contained unit of code that can be imported and used in other parts of the code. Modules in JavaScript can be created using the export keyword. You can export a single function, object, or class by adding export before the declaration.
  2. Import a module: To use a module in another part of your code, you can use the import keyword. You need to specify the path to the module you want to import and assign a variable name to the imported module.
  3. Define a class: A class is a blueprint for creating objects that defines their properties and methods. In JavaScript, classes are defined using the class keyword and can be used to create objects with the new operator.

Here is an example of how to use a module to export and import a class:

File: Person.js

class Person {
  constructor(name) { = name;

  greet() {
    console.log(`Hello, my name is ${}`);

export default Person;

File: main.js

import Person from './Person.js';

const person = new Person('John');
person.greet(); // Hello, my name is John

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply