Modules in JavaScript

Javascript 9 min min read Updated: Mar 09, 2026 Intermediate
Modules in JavaScript
Intermediate Topic 12 of 15

As JavaScript applications grow larger, managing all code in a single file becomes difficult. To solve this problem, JavaScript introduced modules in ES6. Modules allow developers to divide code into smaller, reusable files that can be imported or exported when needed.

Using modules improves code organization, maintainability, and reusability. Each module contains specific functionality that can be shared across different parts of an application.

What are JavaScript Modules?

A JavaScript module is a file that contains code such as variables, functions, or classes that can be exported and reused in other files.

Key Point: Modules help organize JavaScript code into separate reusable files.

Exporting from a Module

The export keyword is used to make variables, functions, or classes available to other modules.

javascript export function greet(name){ console.log("Hello " + name); }
Output

The function is exported and can be used in another JavaScript file.

Importing a Module

The import keyword is used to access exported code from another module.

javascript import { greet } from "./greet.js"; greet("Student");
Output

Hello Student

Exporting Multiple Values

A module can export multiple variables or functions.

javascript export const name = "Rahul"; export function sayHello(){ console.log("Hello"); }

Importing Multiple Values

javascript import { name, sayHello } from "./module.js"; console.log(name); sayHello();
Output

Rahul

Hello

Key Point: Curly braces {} are used when importing specific named exports.

Default Export

A module can also export a single default value using the export default keyword.

javascript export default function greet(){ console.log("Welcome to JavaScript Modules"); }

Importing Default Export

javascript import greet from "./greet.js"; greet();
Output

Welcome to JavaScript Modules

Advantages of JavaScript Modules

  • Improves code organization
  • Encourages code reuse
  • Makes large applications easier to manage
  • Helps maintain clean project structure
Key Point: Modern JavaScript frameworks such as React, Angular, and Node.js heavily rely on modules.

Conclusion

JavaScript modules provide a powerful way to organize code into separate reusable files. By using import and export statements, developers can share functionality between different parts of an application.

Modules are an essential part of modern JavaScript development and are widely used in large-scale web applications.

In the next tutorial, you will learn about Destructuring in JavaScript, which allows developers to extract values from arrays and objects easily.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators