JavaScript Modules: Import and Export

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
Search for a command to run...

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
No comments yet. Be the first to comment.
Every day, millions of users upload photos, videos, stories, and reels to Instagram. From a user's perspective, the process appears simple: select media, apply filters, add a caption, and tap "Post."
Building Offline-First Messaging Apps: How Messages Work Without Internet Modern messaging applications have transformed the way people communicate. Whether it's chatting with friends, collaborating w
In this article we'll explore about the Expo Router and React Navigation and answer which one to use in 2026. If you build mobile apps using React Native, one thing becomes obvious very quickly: Navig

Modern mobile apps are no longer just a collection of screens connected together. Apps like Instagram, WhatsApp, Uber, and Netflix operate at massive scale with millions of users, real time systems, o
In this article we'll be exploring react.js and the things of react.js that makes it popular and stand out among other libraries ( no fight over library vs framework ). We'll go through: What problem

Shkaai
68 posts
In this article we'll understand the JavaScript modules with simple explanations. Imagine having your JavaScript projects growing, keeping all code in one file becomes messy. That’s why modules exist. Let’s go step-by-step in a clear beginner-friendly way.
Imagine writing everything in one file. Small code files seems handleable but what if it becomes 5000+ or 10000+ . That is where the nightmare of developer's begin. Your code becomes hard to find, maintain, naming conflicts arises and reusing code becomes hard to impossible.
That is where Module comes to rescue you from the mess we just discussed.
Think of module as splitting code into separate files, where each file handles one responsibility.
We can see here:
project --
-- math.js
-- greeting.js
-- app.js
Each becomes a module as each file has single responsibility. This makes code clean, organized and reusable. Saving us from a lot of headache.
Remember that a module only exposes things you export. Anything not exported stays private to the file.
for example:
export function add(a, b) {
return a + b;
}
export function multiply(a, b) {
return a * b;
}
Here we are exporting add and multiply function so we can use them in other files.
We can export not only functions but other things as well, let's see them.
Exporting Variables:
export const PI = 3.14;
Exporting Objects:
export const user = {
name: "Sam",
age: 25
};
What we can understand from this is that whatever you export becomes available for other files as well.
Above we learnt that to make one file available we have to export it but that was only 50% of the module story, there is something call importing the modules which complete the entire story. We import the needed files to use them after making them available by exporting them. And this completes the 100% of module story.
for example:
import { add } from "./index.js";
console.log(add(2, 3));
Import Multiple Things
import { add, multiply } from "./math.js";
Import Everything
import * as math from "./math.js";
console.log(math.add(2,3));
console.log(math.multiply(2,3));
When we write this, we are importing everything that is exported from math.js and grouping it into a single object called math and internally it behaves like this:
math = {
add: function,
multiply: function,
}
This is one of the most confusing topics , so let’s break it clearly.
Named Export
You export multiple things by name.
export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}
Import
import { add, subtract } from "./math.js";
Notice the curly braces {}
Because we are importing by name.
A module can have only one default export.
greeting.js
export default function greet(name) {
return "Hello " + name;
}
Import
import greet from "./greeting.js";
No {} needed.
For default export we can do something like this:
export default function greet() {}
You can import it like:
import hello from "./greet.js"
or
import anything from "./greet.js"
In conclusion the name does not matter. (Yup! it does not matter, I find it quite interesting)
Modules provide several advantages when building JavaScript applications.
Better organization
Breaking code into modules makes large projects easier to navigate and understand.
Code reusability
Functions and utilities written in one module can be reused across different parts of the project.
Easier maintenance
If something breaks, developers can quickly locate the module responsible for the issue and fix it.
Team collaboration
Different developers can work on different modules at the same time without interfering with each other’s work.
Encapsulation
Modules allow developers to hide internal implementation details and only expose the necessary parts of the code.
As JavaScript applications grow in size, modules become an essential tool for keeping projects organized, scalable, and maintainable. It save us a lot of time and headache!