JavaScript Modules in 2 mins
JavaScript modules help us organize our code.
Complexity and Abstractions
We are intimately familiar with the concept of complexity. Complex stuff hurts our brains, so we filter out the stuff we don’t need and focus on the stuff we need — this is called abstraction.
Abstractions look at things as a whole as opposed to the components that make up things.
Look at your hand. It is an abstraction of many complicated bones, tendons and cells, but to you, it is just your hand. You know how to use your hand without thinking about the complex bio-mechanics of the hand.
Abstractions are all around us. Take the humble door handle. A door handle has many hidden components. Components have well defined purposes and interactions with other components. When all the components come together, we have a door handle — the door handle is the abstraction. All we think about when using a door handle is how our hand interacts with it. We don’t think about the components that make up the door handle.
Abstracting away complex components from everyday objects allows us to focus on function rather than form. In a world without abstractions, we would be driven to insanity by trying to understand everything — ad infinitum…
We may start at something like a laptop and decompose it into more granular things like motherboard and ram. We would further decompose motherboards into integrated circuits and decompose integrated circuits into sub-integrated circuits. We would then decompose sub-integrated circuits into basic electrical circuits and scores of decompositions later, we are staring atomic and sub-atomic particles in the face. Thus, without abstractions, long journeys down the rabbit hole would be unavoidable.
Take another example. A typical watch has hundreds of components. We don’t need to understand any of those components to understand time. The watch abstracts those details and presents them as a watch face — which we know how to read quickly. The true beauty of abstraction is how transformative it is. Abstractions transform the complex into the simple and intuitive.
Abstractions transform the complex into the simple and intuitive.
Chrome has about 10 million lines of code. It abstracts this complexity from the user via a simple interface.
What are Modules?
A module is a JavaScript file containing JavaScript code. ES6 modules are officially built into JavaScript.
Modules help to divide our programs into distinct chunks. Each chunk or module has a single responsibility. Modules with a single responsibility are easier to reason about and test. Also, they form a solid foundation for improving the software-architecture of your application.
Get Marc Kirk’s stories in your inbox
Join Medium for free to get updates from this writer.
Modules are deceptively easy. We export stuff from a file and then import that stuff.
Named Exports/Imports
To export something from a file — use a named export.
To import what you exported — use named imports within {}
What if two modules export the same name?
When we import accelerate from Ferrari.mjs and Tesla.mjs , we have two versions of accelerate . Naming collisions are prohibited!
To avoid naming collisions, rename your imports.
Default Exports/Imports
Export something as a default with default export.
Then import that default something as any name you want.










