← Back to JavaScript
23

ESM modules: organize JavaScript by responsibility

Learn how to split code into reusable files with `export` and `import`, avoiding hidden dependencies and improving maintainability, testing and scalability.

📘 Theory

Modular thinking: one responsibility per file

A module is not only a file. It is also a clear contract with the rest of the system.

Think of each module as a box with a public API. What you export is what other parts of the app are allowed to know about.

When you split code by domain such as formatting, validation, API or UI, maintenance depends less on personal memory.

1

Contract

2

Encapsulation

3

Reuse

4

Scalability

Exports, imports and explicit dependencies

Define what leaves the module and what enters from other modules.

Named exports are great when one file offers several utilities. Default exports are useful when the module revolves around one main entity.

The real benefit is explicit dependency reading: anyone opening the file can see what the module needs and what it offers.

  • Name modules by intention, such as `price`, `auth` or `dates`, not vague labels such as `helpers2`.
  • Keep relative paths coherent so refactors stay easier.
  • Do not export everything by default without a reason.
  • Watch for circular dependencies when modularizing old code.

Default exports versus named exports

Both are valid, but each one fits a different context.

1

Use named exports when a module exposes several utilities or when you want autocompletion and imports to stay more explicit.

2

Use a default export when the module represents one main thing, such as one central function or one class.

🧪 Learn by doing

Example Guided example: a utilities module Separate calculation from interface code by importing a helper from a dedicated module.
Example Interactive demo: a tiny modular architecture Simulate a modular flow with validation, calculation and output formatting so each block has one responsibility.

🏁 Challenges

Challenge Challenge 1: a formatting module Define one exported function that formats names and then use it from another file with an import.

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com