Skip to main content

Command Palette

Search for a command to run...

Mastering JavaScript Modules: A Beginner's Guide to import, export, and Modular Code for React

Updated
4 min readView as Markdown
Mastering JavaScript Modules: A Beginner's Guide to import, export, and Modular Code for React
S

A passionate tech advocate with a strong foundation in Web Development and a deep interest in the evolving Web 3 space. As a Developer Relations (DevRel) professional, I focus on bridging the gap between developers and products, ensuring they have the right tools and support to thrive.

JavaScript has evolved significantly from its early days of script tags and spaghetti code. In modern development, especially with frameworks like React, modular code is not just a best practice, it's essential. This blog is your beginner-friendly guide to understanding JavaScript Modules, the import and export statements, and why mastering them is crucial before diving into React.


🚀 Why JavaScript Modules Matter

Imagine you're building a house. Would you store all the bricks, wood, and tools in one messy pile? No, you'd organize them into labeled sections. Similarly, in programming, we organize code into modules. This makes our code easier to maintain, scale, and reuse.

JavaScript modules let you break your code into multiple files and reuse them by importing and exporting specific parts.


📦 What is a JavaScript Module?

A module is any JavaScript file that exports something and can import from other files. Modules help in splitting code into logical pieces and sharing code between different files.

Before modules, developers relied on global variables and IIFEs (Immediately Invoked Function Expressions) to simulate modularity, which often led to conflicts and unmanageable code.

Now, with ES6 Modules, we can write:

// mathUtils.js
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;

And use it in another file:

// app.js
import { add, subtract } from './mathUtils.js';

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

✨ The export Keyword

You can export variables, functions, or classes from a module using two methods:

1. Named Export

You can export multiple values by name.

// utils.js
export const greet = (name) => `Hello, ${name}`;
export const age = 25;

2. Default Export

Each module can have only one default export.

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

🔄 The import Keyword

You can use import to bring in code from other modules.

1. Importing Named Exports

import { greet, age } from './utils.js';

2. Importing with Alias

import { greet as sayHello } from './utils.js';
sayHello('John');

3. Importing All as an Object

import * as utils from './utils.js';
console.log(utils.greet('Alice'));

4. Importing Default Export

import log from './logger.js';
log('This is a log message');

🧠 Real-World Example: Modular Todo App

Let’s say you are building a simple Todo app. You can break it into different modules:

// todos.js
export const addTodo = (todo) => { /* logic here */ };
export const deleteTodo = (id) => { /* logic here */ };

// ui.js
export const renderTodos = (todos) => { /* logic here */ };

// index.js
import { addTodo, deleteTodo } from './todos.js';
import { renderTodos } from './ui.js';

addTodo('Learn Modules');
renderTodos([]);

This keeps the code clean, reusable, and ready for scaling.


🛠️ Modules in the React World

React relies heavily on JavaScript modules. Every component is a module that you export and import where needed:

// Header.js
export default function Header() {
  return <h1>My App</h1>;
}

// App.js
import Header from './Header';

function App() {
  return (
    <div>
      <Header />
    </div>
  );
}

Without understanding modules, working with React becomes unnecessarily confusing.


⚠️ Common Mistakes to Avoid

  • Forgetting the file extension (.js) or wrong path in import.

  • Mixing default and named exports incorrectly.

  • Importing with incorrect variable names.

Always remember:

// Correct
import { greet } from './utils.js';
// Wrong
import greet from './utils.js'; // Only works for default export

🧭 Best Practices

  • Use default export when exporting one main thing from a file.

  • Use named exports when you have multiple utilities/functions.

  • Use consistent naming conventions.

  • Organize your code into modules based on functionality, not file type.


✅ Conclusion

JavaScript modules, import, and export are foundational for clean, efficient, and scalable code, especially in the React ecosystem. Understanding them allows you to write modular applications, reuse code easily, and collaborate better with teams.

🔑 Key Takeaways:

  • Use modules to organize your code.

  • Know when to use named vs. default exports.

  • Master imports for maximum code reusability.

More from this blog

D

Dev Diaries - Shivam

33 posts

A journal of insights, stories, and tutorials from the world of web development, Web 3, and developer relations (DevRel).