codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

54 / 65 topics
50JavaScript Template Literals51JavaScript Destructuring52JavaScript Spread & Rest Operators53JavaScript Default Parameters54JavaScript Modules (import/export)55JavaScript Iterators and Generators56JavaScript Proxies
Tutorials/JavaScript/JavaScript Modules (import/export)
🌐JavaScript

JavaScript Modules (import/export)

Updated 2026-05-12
30 min read

JavaScript Modules (import/export)

JavaScript modules are a powerful feature introduced in ECMAScript 6 (ES6) that allow you to organize your code into separate files and manage dependencies more effectively. This makes your codebase cleaner, easier to maintain, and promotes reusability across different parts of an application.

In this tutorial, we'll explore how to use import and export statements to structure your JavaScript applications. We'll cover both named exports and default exports, understand the differences between them, and see how they can be used in practical scenarios.

Introduction

As your JavaScript projects grow in size, managing a single file with all your code becomes increasingly challenging. Modules help by splitting your code into multiple files, each responsible for a specific part of functionality. This not only makes the code easier to manage but also promotes code reuse and reduces duplication.

ES6 modules are supported in modern browsers and Node.js environments, making them a reliable choice for structuring your JavaScript applications.

Core Content

Exporting Modules

There are two primary ways to export values from a module: named exports and default exports. Let's explore each one with examples.

Named Exports

Named exports allow you to export multiple values from a single module using the export keyword followed by the variable, function, or class name.

math.js
1export const add = (a, b) => a + b;
2export const subtract = (a, b) => a - b;

In this example, we've exported two functions, add and subtract, from the math.js file.

Default Exports

Default exports allow you to export a single default value from a module. This is useful when you want to export one primary function or class per module.

greeting.js
1const greet = (name) => `Hello, ${name}!`;
2export default greet;

Here, we've exported the greet function as the default export from the greeting.js file.

Importing Modules

To use the values exported from a module in another file, you can use the import statement. There are several ways to import named exports and default exports.

Importing Named Exports

You can import named exports using curly braces {} followed by the names of the exports.

app.js
1import { add, subtract } from './math.js';
2
3console.log(add(5, 3)); // Output: 8
4console.log(subtract(10, 4)); // Output: 6

In this example, we've imported the add and subtract functions from the math.js file.

Importing Default Exports

You can import a default export using any valid variable name. It's common to use the same name as the exported function or class for clarity.

app.js
1import greet from './greeting.js';
2
3console.log(greet('Alice')); // Output: Hello, Alice!

Here, we've imported the default greet function from the greeting.js file.

Importing Both Named and Default Exports

You can import both named exports and a default export in a single statement. The default export comes first, followed by the named exports inside curly braces.

app.js
1import greet, { add } from './greetingAndMath.js';
2
3console.log(greet('Bob')); // Output: Hello, Bob!
4console.log(add(7, 2)); // Output: 9

In this example, we've imported a default export greet and a named export add from the greetingAndMath.js file.

Combining Export Statements

You can also combine multiple exports in a single statement using the export keyword followed by curly braces {}.

math.js
1const add = (a, b) => a + b;
2const subtract = (a, b) => a - b;
3
4export { add, subtract };

This is equivalent to exporting each value individually as shown earlier.

Re-exporting

Sometimes, you might want to re-export an import from another module. This can be done using the export keyword followed by the from keyword.

mathOperations.js
1export { add, subtract } from './math.js';

In this example, we've re-exported the add and subtract functions from the math.js file into the mathOperations.js file.

Circular Dependencies

Circular dependencies occur when two or more modules depend on each other directly. While JavaScript allows circular dependencies, they can lead to unexpected behavior and are generally best avoided. If you encounter a circular dependency, consider restructuring your code to eliminate it.

Practical Example

Let's create a simple application that uses modules to organize its functionality. We'll create a module for mathematical operations and another for greeting messages.

Step 1: Create the Math Module

Create a file named math.js with the following content:

math.js
1export const add = (a, b) => a + b;
2export const subtract = (a, b) => a - b;

Step 2: Create the Greeting Module

Create a file named greeting.js with the following content:

greeting.js
1const greet = (name) => `Hello, ${name}!`;
2export default greet;

Step 3: Create the Main Application File

Create a file named app.js and import the modules we just created:

app.js
1import { add, subtract } from './math.js';
2import greet from './greeting.js';
3
4console.log(greet('Alice')); // Output: Hello, Alice!
5console.log(add(10, 5)); // Output: 15
6console.log(subtract(20, 8)); // Output: 12

Step 4: Run the Application

To run this application, you can use Node.js. Make sure you have Node.js installed on your machine.

Terminal
$ node app.js
Hello, Alice!
15
12

This will output the results of the imported functions and the greeting message.

Summary

  • Named Exports: Export multiple values from a module using export.
  • Default Exports: Export a single default value from a module using export default.
  • Importing Named Exports: Use curly braces {} to import named exports.
  • Importing Default Exports: Import the default export using any valid variable name.
  • Combining Export Statements: Combine multiple exports in a single statement.
  • Re-exporting: Re-export an import from another module using export ... from.
  • Circular Dependencies: Avoid circular dependencies to prevent unexpected behavior.

What's Next?

Now that you've learned about JavaScript modules, the next topic is "JavaScript Iterators and Generators." These features allow you to work with iterable data structures and create custom iteration logic. Understanding iterators and generators will be beneficial as you delve deeper into more advanced JavaScript concepts.

Stay tuned for the next tutorial where we'll explore how to use iterators and generators in your JavaScript applications!


PreviousJavaScript Default ParametersNext JavaScript Iterators and Generators

Recommended Gear

JavaScript Default ParametersJavaScript Iterators and Generators