Skip to main content

Command Palette

Search for a command to run...

Understanding Modules in JavaScript

Updated
•3 min read•View as Markdown

As your JavaScript code grows, everything starts getting messy.

At first, writing everything in a single file feels fine.
But as the project grows:

  • files become too long

  • code becomes hard to manage

  • finding bugs becomes difficult

This is where modules come in.


Why Modules Are Needed

Imagine writing an entire project in one file:

// user logic
// product logic
// payment logic
// API calls
// UI logic

Everything is mixed together.

Problems:

  • hard to read

  • hard to maintain

  • difficult to reuse code

Modules solve this by breaking code into smaller, focused files.

https://images.openai.com/static-rsc-4/AAMuER-4j7cexVLJuCpPHo1QFKrBmKsWEFpHYTYCjHrA0lV-dgXwcT6a9txFflijCVRzjhedElSNdv6oN2qtVbXMMfigYnqf0YjKNojuZPLoHpGCpb8GuNuJctpXe-R2167orUHPxmv82-Oq8TnxuocVMDzIUy-6TD9N1TR4ohr3HTPKlMtdNhlu6fU7xd1E?purpose=fullsize

What is a Module?

A module is simply:

a file that contains specific code and can be reused

Each module focuses on one responsibility.

Example:

  • user.js → user-related logic

  • auth.js → authentication

  • api.js → API calls


Exporting Functions or Values

To use something from a module, we need to export it.

Example:

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

Here:

  • add is exported

  • other files can use it


Importing Modules

To use exported values, we import them.

// main.js
import { add } from "./math.js"

console.log(add(2, 3))

Now the function is reusable across files.

https://images.openai.com/static-rsc-4/92DL594xcZesAPKRHF4hGUFbC6UW9fOvJj1r6x_JUw9103-4tGsryegjYutcOUSxDNnlKl5Onbhlkx6I_6qdW_xpe5VwjsQvvEfkIZhAAhYBGVEFdq0PX-mF35cg-PIxu_FHrjO_RxrwJTddfzCxpx5smWxf_ruBRRXGuINDtNrsbpVovQjravmzN7q4K3lc?purpose=fullsize

Default vs Named Exports

There are two ways to export:

Named Export

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

Import:

import { add } from "./math.js"

Default Export

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

Import:

import add from "./math.js"

Key Difference

  • Named → must use same name

  • Default → can use any name


Benefits of Modular Code

Modules make code:

  • easier to read

  • easier to maintain

  • reusable

  • organized

Instead of one large file, you get smaller manageable pieces.


Real-World Thinking

In real projects:

  • different teams work on different modules

  • bugs are easier to isolate

  • features are easier to update

Without modules, projects become difficult to scale.


How to Think About Modules

Instead of writing everything together:

Think like this: one file = one responsibility

This keeps code clean and structured.


One Line Summary

Module = a separate file that contains reusable code


Conclusion

Modules solve the problem of messy and unstructured code by breaking it into smaller, reusable parts. They make projects easier to manage, understand, and scale.

Understanding modules is important for writing clean and maintainable JavaScript code.