Featured Article

Simple JavaScript Module Example: A Beginner’s Guide

Adam Jul 13, 2026

Welcome to our comprehensive guide on creating a simple JavaScript module. If you're new to JavaScript modules, don't worry; by the end of this article, you'll be equipped to create and use your own modules with confidence. Let's dive in!

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

A JavaScript module is simply a file that contains reusable code. It's a powerful way to organize your JavaScript projects, promote code reuse, and maintain a clean, efficient codebase. In this guide, we'll create a simple module, explain its components, and demonstrate how to use it.

JavaScript Basics | Chapter 1 πŸ“– | Part 1
JavaScript Basics | Chapter 1 πŸ“– | Part 1

Understanding JavaScript Modules

Before we dive into creating our own module, let's quickly understand what makes up a JavaScript module.

JavaScript Template Literals: Write Cleaner & Dynamic Strings πŸ“
JavaScript Template Literals: Write Cleaner & Dynamic Strings πŸ“

At its core, a module consists of three key components:

  • Exports: The code you want to share with other modules.
  • Imports: The code you want to borrow or use from other modules.
  • Default Exports: A special kind of export that lets you export a single value or function as a default.
JavaScript template literals
JavaScript template literals

Creating a Simple JavaScript Module

Now that we understand the basics, let's create a simple module named 'mathUtils.js'. This module will have two exportable functions: 'add' and 'subtract'.

Here's the code for our 'mathUtils.js' module:

HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet

export function add(a, b) {
  return a + b;
}

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

Using a JavaScript Module

To use our 'mathUtils' module in another JavaScript file, we'll use the 'import' keyword. Let's create a file named 'app.js' that imports and uses our 'mathUtils' module.

Here's the code for our 'app.js' file:

JavaScript Basics for Beginners | Learn JS Step by Step
JavaScript Basics for Beginners | Learn JS Step by Step

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

console.log(add(3, 5)); // Outputs: 8
console.log(subtract(10, 4)); // Outputs: 6

Understanding Default Exports

Default exports allow us to export a single value or function that can be imported without specifying the property name. Let's modify our 'mathUtils.js' module to include a default export:

a calculator is shown with the numbers below it
a calculator is shown with the numbers below it
JavaScript Basics | Chapter 1 πŸ“– | Part 2
JavaScript Basics | Chapter 1 πŸ“– | Part 2
CSV To HTML Table In Javascript
CSV To HTML Table In Javascript
Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial
JavaScript Full Notes Every Beginner Must Save πŸ’‘
JavaScript Full Notes Every Beginner Must Save πŸ’‘
Dynamic Class Toggle in DOM
Dynamic Class Toggle in DOM
login form html css
login form html css
JavaScript calculator projet
JavaScript calculator projet
Toggle Fullscreen in Javascript
Toggle Fullscreen in Javascript
How to Create a Function in JavaScript: Step-by-Step Guide πŸš€
How to Create a Function in JavaScript: Step-by-Step Guide πŸš€
Javascript Cheet cheet
Javascript Cheet cheet
Best JavaScript Projects for Beginners to Practice
Best JavaScript Projects for Beginners to Practice
Magic Navigation Menu, HTML and #CSS
Magic Navigation Menu, HTML and #CSS
Javascript Loops & Iterations
Javascript Loops & Iterations
"Master JavaScript's Slice Method οΏ½: Easy Array & String Slicing Tips! πŸš€ #CodingTips #WebDev"
"Master JavaScript's Slice Method οΏ½: Easy Array & String Slicing Tips! πŸš€ #CodingTips #WebDev"
an info sheet with different types of web pages
an info sheet with different types of web pages
πŸ“˜ JavaScript Basics| Linking JS File to HTML | Chapter 2 πŸ“– Part 2
πŸ“˜ JavaScript Basics| Linking JS File to HTML | Chapter 2 πŸ“– Part 2
Javascript Fundamental
Javascript Fundamental
Promise Intro in JavaScript
Promise Intro in JavaScript
CSS Notes for Beginners (Easy + Quick Guide) 🎨
CSS Notes for Beginners (Easy + Quick Guide) 🎨

Here's the updated code for our 'mathUtils.js' module:

export function add(a, b) {
  return a + b;
}

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

export default {
  add,
  subtract,
};

Now, we can import the default export in our 'app.js' file like this:

import mathUtils from './mathUtils.js';

console.log(mathUtils.add(3, 5)); // Outputs: 8
console.log(mathUtils.subtract(10, 4)); // Outputs: 6

Organizing JavaScript Modules

In larger projects, you'll often have many modules, making organization crucial. A common approach is to structure your project into folders, with each folder containing related modules. Here's an example:

/
|-- src/
|   |-- math/
|   |   |-- mathUtils.js
|   |   |-- ...
|   |-- ui/
|   |   |-- button.js
|   |   |-- ...
|   |-- app.js
|-- index.js

In this example, the 'math' folder contains math-related modules, the 'ui' folder contains user interface-related modules, and 'app.js' is the main entry point for our application.

With a clear understanding of JavaScript modules and a simple example under your belt, you're ready to start building and organizing your own projects. Happy coding!