DEV Community

Cover image for JavaScript - Use Modules for Better Code Organization
Keyur Ramoliya
Keyur Ramoliya

Posted on

JavaScript - Use Modules for Better Code Organization

JavaScript modules provide a way to encapsulate and organize code into separate files or modules, helping you manage complex applications and maintainability. Modules allow you to split your code into smaller, reusable parts with well-defined interfaces. This promotes cleaner code, reduces potential naming conflicts, and enhances code reusability. There are several module formats available, but we'll focus on ES6 modules, which have become a standard in modern JavaScript:

  • Creating a Module:

In an ES6 module, each JavaScript file is treated as a module. You can export functions, variables, or classes using the export keyword:

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

   // main.js
   import { add } from './math.js';

   const result = add(2, 3);
   console.log(result); // 5
Enter fullscreen mode Exit fullscreen mode
  • Default Exports:

You can also export a single "default" value from a module, which is useful for exporting a single function or class as the module's primary export:

   // calculator.js (module file)
   export default function add(a, b) {
     return a + b;
   }

   // main.js
   import add from './calculator.js';

   const result = add(2, 3);
   console.log(result); // 5
Enter fullscreen mode Exit fullscreen mode
  • Importing Modules:

You can import exported values from other modules using the import statement. This allows you to use functionality from other files within your application:

   // main.js
   import { add } from './math.js';

   const result = add(2, 3);
   console.log(result); // 5
Enter fullscreen mode Exit fullscreen mode
  • Module Benefits:

    • Encapsulation: Modules help you encapsulate functionality, reducing the risk of global variable conflicts and promoting a more organized code structure.
    • Reusability: You can easily reuse modules across different application parts or in other projects.
    • Maintenance: Modules make it easier to manage and update individual pieces of code without affecting the entire application.
    • Dependency Management: You can clearly define dependencies between modules, making it easier to understand the relationships between different parts of your application.

Using JavaScript modules is a best practice for structuring and organizing your code in modern JavaScript applications. It promotes modularity, maintainability, and reusability, all of which are essential for building scalable and maintainable software.

Top comments (1)

Collapse
 
jimnayzium profile image
Jim Nayzium

In my Sveltekit project I import fully exported Classes, but then only uses a few methods from the class in any given .svelte component. The thought struck me that I am importing the entire class, while easier on the organization and number of import statements, may well be fattening the file size of the finished build.

I am new to the concept of tree-shaking coming from a long and very novice/hobby level of PHP coding. Should I be thinking of my "Class" files in module/based JS applications as just simple FILES now?

So organizationally, if I have a set of functions that I normally would have put into a class called "LogoutService.js" and it has various methods to control the logout process depending on whether or not the user has logged in via google/github or email+password, should I instead just named a file LogoutService.js that has a handful of export async function myMethod(); type thing?

Then, when importing just import the used functions instead of the entire class?