Skip to main content

Command Palette

Search for a command to run...

JavaScript Modules: Import and Export Explained

Updated
3 min read
JavaScript Modules: Import and Export Explained

Hello readers,

Welcome back to another blog. In the previous blogs, we’ve been learning different JavaScript concepts that make our code cleaner and more powerful. But as your project grows, something even more important comes into play — code organization.

If you’ve ever worked on a medium or large project, you know how messy things can get when everything is written in a single file

Today, we’ll fix that problem by understanding JavaScript Modules — one of the most important concepts in modern JavaScript.

The Problem: Unorganized Code

Let’s say you're building a project like a blog app or e-commerce app.

You might end up with something like this:

// utils, API calls, UI logic — everything in one file 

function calculateTotal(price, tax) {
  return price + tax;
}

function fetchProducts() {
  // API logic
}

function renderUI() {
  // UI logic
}

Problems with this approach:

  • Hard to read

  • Difficult to debug

  • Not reusable

  • Becomes messy as the project grows

This is where modules come into play.

What are JavaScript Modules?

A module is simply a separate file that contains specific functionality.

Instead of writing everything in one file, we split the code into multiple files.

Example:

  • math.js → math functions

  • api.js → API calls

  • ui.js → UI logic

This makes your code:

  • Cleaner

  • Easier to manage

  • Reusable

Exporting Functions or Values

To use code from one file in another, we need to export it.

Named Export

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

export const PI = 3.14;

Importing Modules

Now we can import these into another file:

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

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

Notice:

  • Curly braces {} are used for named imports

  • File path is required (./math.js)

Default vs Named Exports

This is where many beginners get confused, so let’s break it down simply.

Default Export (Only One Per File)

// greet.js
export default function greet(name) {
  return `Hello ${name}`;
}

importing:

import greet from './greet.js';

console.log(greet("Abhishek"));

No {} needed
You can rename it while importing

Why Modules are Important

  1. Better Maintainability

  2. Reusablity

  3. Scalability

  4. Team Collaboration

How Modules Improve Code Structure

Instead of this:

// everything.js
function login() {}
function fetchUser() {}
function renderDashboard() {}

You can do this:

auth.js
api.js
ui.js

// auth.js
export function login() {}

// app.js
import { login } from './auth.js';

Now the project feels structured like a real-world application.

💡
Modules work with <script type="module"> in browsers

That’s it for this blog. This concept might feel simple, but it’s extremely powerful when used in real projects. Start applying it in your projects, and you’ll immediately notice the difference.

I will see you in another interesting and useful blog. Till then...

Stay consistent and keep grinding....

Peace ✌️