Skip to main content

Command Palette

Search for a command to run...

JavaScript Modules: Import and Export Explained

Updated
5 min readView as Markdown

Imagine you have a massive box of LEGOs. If you dump them all on the floor in one giant pile, finding that one tiny steering wheel becomes impossible. You’re stepping on sharp bricks, and everything is a mess.

JavaScript Modules are like taking those LEGOs and organizing them into smaller, labeled kits. One kit is just for wheels, one is for windows, and another is for the engine. When you want to build a car, you just grab the specific kits you need.


The image shows the structure of a JavaScript application through file dependencies, beginning with a central main.js entry point. It illustrates how utility files like math.js are imported into higher-level modules such as fetchUser.js or Dashboard.js.

The diagram distinguishes between Named Imports and Default Imports, highlighting interactions between module types (ESM and CommonJS). This visual emphasizes how modularity avoids a single massive script by dividing logic into specialized, interconnected components.

Without modules, JavaScript code becomes "global scope pollution," where different scripts can accidentally overwrite each other's variables. This lack of structure leads to "spaghetti code," making it nearly impossible to manage dependencies or find specific logic within massive, monolithic files.

1. The "Messy Room" Problem (Why we need Modules)

Before modules, JavaScript was like a room where everyone shared the same desk. If I put a blue pen on the desk and named it myPen, and then you came along and put a red pen down also named myPen, my pen would suddenly turn red!

In coding, we call this Global Scope Pollution.

  • The Conflict: If two different parts of your website use a variable named user, they will fight over it.

  • The Confusion: When you have 2,000 lines of code in one file, it’s like reading a book with no chapters and no page numbers. You get lost fast!


2. Exporting: "Giving a Gift"

To share code between files, you have to "Export" it. Think of this like putting a toy in a box and sticking a label on it so someone else can use it.

The "Label Every Item" Way (Named Exports)

You can export as many things as you want from one file. You just have to give them a name.

JavaScript

//Export" (The Tool Belt)
//Imagine you have a tool belt with a hammer, a screwdriver, and a wrench. You want to share them individually. 

// tools.js
export const hammer = "🔨";
export const wrench = "🔧";

export function fixThing() {
  return "All fixed!";
}

The "One Big Gift" Way (Default Exports)

Every file can have one special "main" item. It’s like the "Featured Toy" of the box.

This is the star of the show!

, how do you get it into your other file? You "Import" it!

Picking Specific Items (Named Imports)

When you use named exports, you have to use curly braces {}. It’s like saying, "From the tools box, I only want the hammer and the wrench."

JavaScript

import { hammer, wrench } from './tools.js';

console.log(hammer); // 🔨

Taking the Main Item (Default Imports)

Since there is only one default export, you don't need curly braces. You can even give it a new name if you want!

import myFriendlyRobot from './robot.js';

console.log(myFriendlyRobot); // 🤖

. Named: What's the Difference?

Think of a Named Export like a spice rack. You might want just the "Salt," "Pepper a pizza, you don't say "I'll take the pizza's crust, the pizza's cheese, and the pizza's sauce" separately—you just get the Pizza.

Feature

Named Exports (The Spice Rack)

Default Exports (The Pizza)

How many?

Lots!

Only one per file.

The Name

Must match exactly.

You can call it whatever you want.

The Symbol

Uses { } brackets.

No brackets needed.


5. Why This Makes You a Super Coder

  1. Safety: Because each module is its own "room," your variables won't accidentally overwrite someone else's.

  2. Organization: Your "Main" file stays clean. It just lists the imports at the top, like a table of contents for a book.

  3. Speed: Browsers are smart. If you have 100 modules but only use 5, the browser can focus on making those 5 work perfectly.

  4. Teamwork: If you are working with a friend, you can work on the scoring.js module while they work on the graphics.js module. You won't get in each other's way!

The Golden Rule

  • Export = Making your code available to others.

  • Import = Bringing someone else's code into your file.

By using modules, you turn your "spaghetti code" into a neatly organized LEGO set. Happy building!

1 views