Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Basic JavaScript

JS Tutorial JS Syntax JS Variables JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Dates JS Arrays JS Typed Arrays JS Sets JS Maps JS Math JS RegExp JS Data Types JS Errors JS Events JS Programming JS References JS UTF-8 Characters JS Versions

JS Advanced

JS Functions JS Objects JS Classes JS Iterations JS Asynchronous JS Modules JS HTML DOM JS Windows JS Web API JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JavaScript Modules

What are Modules?

Modules allow you to break up code into separate files.

Modules are chunks of self-contained code.

You use import and export to interchange functionalities between JavaScript modules.


How to Use Modules

Modules are imported from external files with the import statement.

Modules also rely on type="module" in the <script> tag.

Example

<script type="module">
import message from "./message.js";
</script>

Try it Yourself »

Note

Modules operate in strict mode by default.

Modules must be stored on a server.


Export

To share code with other files, you use the export keyword.

A module can have multiple named exports and, optionally, one default export.


Named Exports

Let us create a file named person.js, and fill it with the things we want to export.

You can create named exports two ways. In-line individually, or all at once at the bottom.

In-line individually:

person.js

export const name = "Jesse";
export const age = 40;

All at once at the bottom:

person.js

const name = "Jesse";
const age = 40;

export {name, age};

Default Exports

Let us create another file, named message.js, and use it for demonstrating default export.

You can only have one default export in a file.

Example

message.js

const message = () => {
const name = "Jesse";
const age = 40;
return name + ' is ' + age + 'years old.';
};

export default message;


Import

You can import modules into a file in two ways, based on if they are named exports or default exports.

Named exports are constructed using curly braces. Default exports are not.


Import Named Exports

You must use the exact names of the exported variables or functions, enclosed in curly braces:

Example

Import named exports from the file person.js:

import { name, age } from "./person.js";

Try it Yourself »


Import Default Exports

You can give a default export any name you like, during import, without using curly braces:

Example

Import a default export from the file message.js:

import message from "./message.js";

Try it Yourself »


Importing Everything

You can import all named exports from a module as a single object using the * syntax.

// Import all named exports from utilities.js
import * as utils from "./utilities.js";

Benefits of Using Modules

Modules help structuring a codebase logically by separating concerns. This is essential for larger projects.

Modules prevent naming conflicts by keeping variables and functions within the module scope.

A module can be easily reused across different parts of an application and even in entirely new projects.

Small files are easier to maintain and debug, as you only need to focus on one piece of functionality.

Modules make explicit dependencies and are automatically loaded, reducing the risk of missing code.


Note

Modules only work with the HTTP(s) protocol.

A web-page opened via the file:// protocol cannot use import / export.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.