Skip to content

Code Organization and Reuse

Organizing your code effectively is vital for maintaining readability, scalability, and reusability in modern applications. In this article, we’ll explore key concepts like export and import syntax, the difference between named and default exports, and how to work with modules in modern environments such as browsers and bundlers like Webpack and Vite.

JavaScript provides the export and import keywords to share and reuse code across different files or modules. These keywords enable modular programming, ensuring that code is not duplicated and stays well-structured.

First, define the functions, objects, or variables to export:

mathUtils.js
export function add(a, b) {
return a + b;
}
export const PI = 3.14;

Then, import the exported items in another file:

app.js
import { add, PI } from "./mathUtils.js";
console.log(add(2, 3)); // 5 console.log(PI); // 3.14
  • Export allows a file to expose its functionality or data.
  • Import allows other files to access and use the exported content.

JavaScript modules support two types of exports: named exports and default exports. Understanding the difference can help you choose the right export strategy.

Named exports allow you to export multiple items from a single file. These exports must be imported using their exact names.

utilities.js
export function greet(name) {
return `Hello, ${name}!`;
}
export const version = "1.0.0";
// Importing named exports
import { greet, version } from "./utilities.js";
console.log(greet("Alice")); // Hello, Alice!
console.log(version); // 1.0.0

With named exports, you can also use the as keyword to rename imports during importation.

import { greet as sayHello } from "./utilities.js";
console.log(sayHello("Bob")); // Hello, Bob!

Default exports allow you to export a single item from a file. You can name the imported item however you want in the importing file.

calculator.js
export default function multiply(a, b) {
return a * b;
}
// Importing a default export import multiply from './calculator.js';
console.log(multiply(4, 5)); // 20
  • Use named exports when a module has multiple exports or when you want explicit and descriptive import behavior.
  • Use default exports for modules where only one major functionality is exported, such as utility functions or components.

Working with Modules in Modern Browsers and Bundlers

Section titled “Working with Modules in Modern Browsers and Bundlers”

JavaScript modules have become the standard for building modern web applications. Modules work seamlessly in both modern browsers and popular bundlers like Webpack and Vite.

Modern browsers natively support JavaScript modules using the type="module" attribute in the <script> tag.

  • Module imports in browsers must use a relative or absolute file path (e.g., ./module.js).
  • By default, modules are scoped to strict mode ('use strict').
  • Imports are asynchronous, meaning dependencies are fetched dynamically.

Bundlers like Webpack and Vite simplify working with modules in production environments by:

  • Optimizing and bundling: Combines multiple files into a single bundle for performance.
  • Converting modules for backward compatibility with older environments (e.g., CommonJS to ES Modules).

Install Vite in your project:

Terminal window
npm install vite

Create a module file and an entry point:

mathUtils.js:

export function square(x) {
return x * x;
}

main.js:

import { square } from "./mathUtils.js";
console.log(square(4)); // 16

Run the development server for testing:

Terminal window
npm run dev
  • Better module resolution and dependency management.
  • Built-in optimizations like tree-shaking (removing unused exports).
  • Integrated support for modern JavaScript features, CSS, and more.

By mastering export and import syntax, understanding named vs default exports, and leveraging modern module systems, you can organize your code more effectively and reuse it in a clean, scalable manner.