Day 15: JavaScript Modules - Mastering Code Organization

As our JavaScript projects grow, organizing code into modular pieces becomes essential. JavaScript Modules allow us to separate code into logical, reusable files, making our projects more maintainable and scalable. This post will explore the basics of modules, how to create and import them, and best practices for using them.

Why Use Modules?

Modules are essential in modern JavaScript as they:

  • Improve code organization by separating functions and variables into distinct files.

  • Enable code reuse by making it easy to import functionality across different files.

  • Reduce dependencies by encapsulating specific functionality within a single file.

Learn more about the importance of modular JavaScript code here: MDN Web Docs - JavaScript Modules

Creating and Exporting Modules

In JavaScript, modules are created by defining functions, variables, or classes in a separate file and using the export keyword to make them accessible to other files. Here’s a simple example:

// mathUtils.js
function square(number) {
    return number * number;
}

function cube(number) {
    return number * number * number;
}

export { square, cube };

In this example, square and cube are named exports, allowing us to import these functions into other files selectively.

More on Named Exports:
JavaScript.info - Export and Import

Importing Modules

To access functions or variables from another module, we use the import keyword:

// main.js
import { square, cube } from './mathUtils.js';

console.log(square(3)); // Output: 9
console.log(cube(2));   // Output: 8

The import statement pulls only the necessary code, which helps with performance in larger applications.

Default Exports

If a module has a primary function or variable, we can export it as a default export, making it easier to import without specifying a name in curly braces:

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

// main.js
import greet from './greet.js';

console.log(greet('JavaScript')); // Output: Hello, JavaScript!

This approach is often used when there’s a single purpose for a module, making the import cleaner.

Learn more about Default Exports:
freeCodeCamp - JavaScript ES6 Modules

Importing Everything with *

When a module has multiple exports and we want to import them all, we can use *:

import * as mathUtils from './mathUtils.js';

console.log(mathUtils.square(4)); // Output: 16
console.log(mathUtils.cube(3));   // Output: 27

This can be helpful when we need to access several functions from one file, without listing each individually.

Best Practices for Using Modules

  1. Single Responsibility: Keep each module focused on a single feature or related features.

  2. Meaningful Naming: Name functions and variables clearly to make code easier to understand.

  3. Avoid Overuse of Default Exports: When possible, prefer named exports to make it clear what’s imported.

For more tips, check out: JavaScript.info - Modules Best Practices

Conclusion

JavaScript modules simplify coding by breaking down larger applications into manageable parts. They enable us to reuse code efficiently and improve the readability and maintainability of projects. Understanding modules is a key step toward mastering modern JavaScript development.


With these insights, you’re ready to start modularizing your own code! For an in-depth overview and more examples, explore these resources:

  1. MDN Web Docs - JavaScript Modules

  2. freeCodeCamp - JavaScript ES6 Modules

  3. JavaScript.info - Export and Import

Happy coding!

10
Subscribe to my newsletter

Read articles from Stanley Owarieta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Stanley Owarieta
Stanley Owarieta

𝗔𝘀𝗽𝗶𝗿𝗶𝗻𝗴 𝗙𝗿𝗼𝗻𝘁𝗲𝗻𝗱 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿 passionate about 𝙅𝙖𝙫𝙖𝙎𝙘𝙧𝙞𝙥𝙩, 𝙘𝙤𝙙𝙞𝙣𝙜, 𝙖𝙣𝙙 𝙗𝙪𝙞𝙡𝙙𝙞𝙣𝙜 𝙢𝙮 𝙛𝙪𝙩𝙪𝙧𝙚 𝙞𝙣 𝙩𝙚𝙘𝙝. Along with my 𝙡𝙤𝙫𝙚 for 𝙛𝙖𝙨𝙝𝙞𝙤𝙣, 𝙜𝙖𝙢𝙞𝙣𝙜, 𝙖𝙣𝙙 𝙡𝙪𝙭𝙪𝙧𝙮 𝙡𝙞𝙫𝙞𝙣𝙜, I have big dreams like 𝙤𝙬𝙣𝙞𝙣𝙜 𝙖 𝙥𝙧𝙞𝙫𝙖𝙩𝙚 𝙟𝙚𝙩 and 𝙡𝙞𝙫𝙞𝙣𝙜 𝙞𝙣 𝙖 𝙡𝙪𝙭𝙪𝙧𝙮 𝙝𝙤𝙢𝙚 𝙤𝙣𝙚 𝙙𝙖𝙮. Since 2021, I’ve invested in 𝗔𝗽𝗽𝗹𝗲, 𝗔𝗺𝗮𝘇𝗼𝗻, 𝗦𝗵𝗼𝗽𝗶𝗳𝘆, 𝗮𝗻𝗱 𝗚𝗼𝗼𝗴𝗹𝗲—working toward financial independence. I also look forward to being a 𝗹𝗼𝘃𝗶𝗻𝗴 𝗳𝗮𝘁𝗵𝗲𝗿 𝗮𝗻𝗱 𝗮 𝗱𝗲𝘃𝗼𝘁𝗲𝗱 𝗽𝗮𝗿𝘁𝗻𝗲𝗿, growing a 𝗺𝗶𝗹𝗹𝗶𝗼𝗻-𝗱𝗼𝗹𝗹𝗮𝗿 𝗯𝗿𝗮𝗻𝗱 together with my 𝗳𝘂𝘁𝘂𝗿𝗲 𝘄𝗶𝗳𝗲. Let’s connect and inspire each other on this journey!