Featured Article

Master Module JavaScript Example: A Complete Guide

Adam Jul 13, 2026

JavaScript modules have become a crucial part of modern web development, enabling developers to break code into reusable chunks. Since the introduction of ECMAScript 6 (ES6), JavaScript has included native support for modules through the `import` and `export` keywords. This article will explore JavaScript modules through an engaging hands-on example.

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

Before diving into the example, let's briefly cover what JavaScript modules are and why they are essential. In simple terms, modules in JavaScript are plain JavaScript files that contain reusable code. They have a clear separation of concerns, making your application more organized and maintainable. With modules, you can write and import small, single-purpose functions, classes, or entire UI components across different files.

Nestjs Module Tutorial with Example for JavaScript Developers | Global & Dynamic Modules
Nestjs Module Tutorial with Example for JavaScript Developers | Global & Dynamic Modules

Creating and Using JavaScript Modules

To demonstrate the power of JavaScript modules, let's create a simple math module. This module will contain two exported functions: `add` and `multiply`.

A Quick Module Design Pattern Example in JavaScript
A Quick Module Design Pattern Example in JavaScript

Create a new file named `math.js` and paste the following code:

```javascript // math.js const add = (a, b) => a + b; const multiply = (a, b) => a * b; // Export the functions export { add, multiply }; ```

Exporting Functions

JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡

In the `math.js` file, we've used named exports to exporting functions. The `export` keyword allows us to make variables, functions, or objects available to other modules. To export multiple items, we use curly braces `{}`.

Now, let's use this math module in another file called `app.js`.

app.js:

How to Create a Function in JavaScript: Step-by-Step Guide 🚀
How to Create a Function in JavaScript: Step-by-Step Guide 🚀

```javascript // app.js import { add, multiply } from './math.js'; console.log(add(2, 3)); // Outputs: 5 console.log(multiply(2, 3)); // Outputs: 6 ```

Importing Functions

In the `app.js` file, we've imported the `add` and `multiply` functions using the `import` keyword. By using curly braces {}, we import specific exports (in this case, the `add` and `multiply` functions) from the math module. The path './math.js' is a relative path to the math module.

Default Exports

DataTypes Javascript
DataTypes Javascript

Besides named exports, JavaScript modules also support default exports. A default export allows you to return a single value from a module.

mathWithDefault.js:

HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet
Javascript Fundamental
Javascript Fundamental
JavaScript Basics | Chapter 1 📖 | Part 1
JavaScript Basics | Chapter 1 📖 | Part 1
Magic Navigation Menu, HTML and #CSS
Magic Navigation Menu, HTML and #CSS
JavaScript calculator projet
JavaScript calculator projet
JavaScript Works in the Browser
JavaScript Works in the Browser
JavaScript and the Netflix User Interface
JavaScript and the Netflix User Interface
JavaScript Template Literals: Write Cleaner & Dynamic Strings 📝
JavaScript Template Literals: Write Cleaner & Dynamic Strings 📝
tg: webguild
tg: webguild
the solution for loops is to create a program that allows you to use numbers and symbols
the solution for loops is to create a program that allows you to use numbers and symbols
an info sheet with different types of web pages
an info sheet with different types of web pages
login form html css
login form html css
JavaScript template literals
JavaScript template literals
Click Events in JavaScript
Click Events in JavaScript
Toggle Fullscreen in Javascript
Toggle Fullscreen in Javascript
a black background with white text that reads a full example
a black background with white text that reads a full example
Dynamic Class Toggle in DOM
Dynamic Class Toggle in DOM
JavaScript Arrow Functions: Write Shorter & Cleaner Code 🚀
JavaScript Arrow Functions: Write Shorter & Cleaner Code 🚀
JavaScript loops by Duomly
JavaScript loops by Duomly
Functions in Javascript
Functions in Javascript

```javascript // mathWithDefault.js const add = (a, b) => a + b; const multiply = (a, b) => a * b; export default { add, multiply }; ```

Importing a Default Export

To import a default export, you can use any variable name as the import alias. Here's how you can import the `mathWithDefault` module:

```javascript // app.js import math from './mathWithDefault.js'; console.log(math.add(2, 3)); // Outputs: 5 console.log(math.multiply(2, 3)); // Outputs: 6 ```

Having explored named and default exports, you now have a solid understanding of how to create and use JavaScript modules.

To take your module skills further, consider exploring dynamic imports, asynchronous modules, and bundlers like Webpack or Parcel. Also, don't forget to learn about Node.js modules, as they share many similarities but have some distinct differences.

Embrace the power of JavaScript modules, and watch your codebase become more modular, maintainable, and scalable. Happy coding!