A Beginner’s Guide to ES Modules in JavaScript

Riya Garg
Women in Technology
2 min readDec 26, 2023

In the dynamic world of JavaScript, where web development evolves at a rapid pace, staying abreast of modern features is essential. One such crucial addition to the language is ECMAScript Modules, commonly known as ES Modules. In this article, we’ll take a beginner-friendly journey into the world of ES Modules and discover how they can enhance the maintainability and scalability of your projects.

Understanding ES Modules

ES Modules, or ECMAScript Modules, are a standard for structuring code in JavaScript applications. They provide a way to encapsulate pieces of code, making it easier to manage dependencies, share functionality across files, and improve overall project organization. Key Features are as below:

  1. Encapsulation: Modules allow you to encapsulate code by creating self-contained units, preventing pollution of the global namespace.
  2. Dependency Management: ES Modules facilitate a clean and efficient way to manage dependencies between different parts of your application.
  3. Export and Import: With the export and import keywords, you can define what parts of a module are accessible to other parts of your code.

Basic Usage

Creating a Module: Let’s start by creating a simple module. Consider a file named utils.js

// utils.js
export function add(a, b) {
return a + b;
}

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

Using the Module: Now, let’s use our utils module in another file:

// main.js
import { add, subtract } from './utils.js';

console.log(add(5, 3)); // Output: 8
console.log(subtract(8, 3)); // Output: 5

Different Ways to Import

  1. Named Imports: You can import specific functions or variables from a module.
import { add, subtract } from './utils.js';

2. Default Import: If a module has a default export, you can import it like this-

// utils.js
export default function multiply(a, b) {
return a * b;
}

// main.js
import multiply from './utils.js';

console.log(multiply(4, 3)); // Output: 12

Working with ES Modules in the Browser

Script Type Module

To use ES Modules in a browser environment, use the type="module" attribute in your script tag. This informs the browser that the script should be treated as an ES Module.

<!-- index.html -->
<script type="module" src="main.js"></script>

Asynchronous Loading with import

ES Modules support asynchronous loading through the import statement. This enables developers to load modules on demand, reducing initial loading times and improving performance.

// main.js
import('./math.js')
.then((module) => {
console.log(module.add(3, 4)); // Outputs: 7
})
.catch((error) => console.error('Error:', error));

Browser Support and Tooling

ES Modules are broadly support in modern browsers, but it’s crucial to consider compatibility for your specific project. Additionally, build tools like Webpack and Parcel provide advanced capabilities for bundling and optimizing ES Modules in larger applications.

ES Modules bring a modern and efficient approach to structuring JavaScript code. By promoting encapsulation, improving dependency management, and providing a clean syntax for exports and imports, ES Modules empower developers to build more maintainable and scalable applications.

--

--

Riya Garg
Women in Technology

Mentor, writer and passionate about everything web.