Skip to main content

Command Palette

Search for a command to run...

JavaScript Modules: Import and Export Explained

Published
6 min read
JavaScript Modules: Import and Export Explained

Have you ever opened a drawer, stuffed with everything like: phone chargers, old receipts, keys you don't recognize, a pen that definitely doesn't work anymore? You know what you're looking for is in there somewhere, but finding it feels like an archaeological dig.

That's what your JavaScript code starts to feel like, when everything lives in one file.

Now let's imagine you started a project with excitement, writing everything in index.js. Two weeks later, that file is 800 lines long. Now you realize that your code is breaking somewhere... You scroll up, scroll down, lose your place, accidentally break something while fixing something else... and slowly, you start losing interest.

Modules are JavaScript's answer to that messy drawer. They let you organize your code into small, focused files, each with a clear purpose. And the way you connect those files? That's what import and export are for.

1. Why Modules Are Needed

Before modules became a standard part of JavaScript, developers had a serious problem, everything lived in one place. It may lead to global scope pollution problem.

Global Scope Pollution Problem :

As applications grew, a single JavaScript file would become thousands of lines. Variables from one part of the code would accidentally overwrite variables in another. Functions with similar names would clash. Debugging felt like headache.

This is known as the global scope pollution problem. When everything is declared globally, everything can interfere with everything else.

Modules solve this by giving each piece of your code its own private space. Variables and functions inside a module are invisible to the outside world, unless you deliberately choose to share them.

2. Exporting Functions or Values

To share something from a module, the first step is exporting, deciding what a module is willing to share with the rest of your app

Think of exporting as putting something on a shelf that others are allowed to pick up.

JavaScript gives you two ways to export: named exports and default exports. We will see both of them in detail further in this article, but here's the core idea of a named export:

In Named export, we need to export each function or variable, which can be imported in other files with their exact name.

Example : Suppose their is a file named as math.js

// math.js

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

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

export const PI = 3.14159;

In above file, add subtract and PI all are made available to other files. Other than that whatever we write in math.js will stay private by default, means no accidental leaks.

We can also do named export in other way as follows :

// math.js

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

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

const PI = 3.14159;

export { add, subtract, PI };

Both approaches do exactly the same thing.

3. Importing Modules

Once something is exported, you can import it into another file.

Importing is how you say, "I need this specific tool from that specific shelf."

// app.js

import { add, PI } from './math.js';

console.log(add(2, 3));   // 5

console.log(PI);           // 3.14159

A few things to notice here:

  • The curly braces { } tell JavaScript exactly which named exports you want.

  • The path ./math.js points to where the module lives. The ./ means "look in the same folder."

  • You only import what you actually need, you don't have to bring in everything the module offers.

If you want everything a module exports under one namespace, you can also do:

import * as MathUtils from './math.js';

console.log(MathUtils.add(4, 5));  // 9

console.log(MathUtils.PI);          // 3.14159

This is especially useful when you're using many functions from a module and want to keep them grouped.

4. Default vs Named Exports

This is a most confusing topic in export and import, let's understand this precisely.

Named Exports

Named exports let you export multiple things from a single file. When importing them, you must use the exact same name which must be wrapped in curly braces.

Example :

// greetings.js

export function sayHello() { 
    return "Hello!"; 
}

export function sayBye() { 
    return "Goodbye!"; 
}
// app.js

import { sayHello, sayBye } from './greetings.js';

The names must match, means sayHello must be imported as sayHello.

Default Exports

A default export is the main thing a module offers.

Each file can only have one default export.

Example :

// logger.js

export default function log(message) {
  console.log(`[LOG]: ${message}`);
}

When importing a default export, you skip the curly braces, and you can give it any name you want.

Example :

js// app.js

import log from './logger.js';
import printMessage from './logger.js'; // this also works!

When to Use Which?

Situation Use
The file has one clear, main purpose Default export
The file provides multiple utilities Named exports
A utility library (like math helpers) Named exports
A single component or class Default export

5. Benefits of Modular Code

Better Organization : Each file has a single, well-defined responsibility. A utils.js file handles utilities. An api.js file handles network calls. You always know exactly where to look.

Easier Maintenance : When something breaks, you don't hunt through a 3,000 line file. You open the relevant module, fix the issue, and move on. Changes in one module don't unexpectedly break another.

Reusability : A well-written module is like a brick, you can snap it into any project that needs it. Write a formatDate.js once, and reuse it across five different applications.

Collaboration-Friendly : Teams can work on different modules simultaneously without stepping on each other's code. Merge conflicts become rare. Code reviews become focused.

Readability : Code that's split into focused, well-named modules reads almost like documentation. The structure itself communicates intent.

6. Conclusion

JavaScript modules aren't just a technical feature, they're a philosophy of writing code that's clean, focused, and built to grow. By using export to share only what's necessary and import to pull in only what's needed, you create applications that are easier to understand, debug, and maintain.