JavaScript modules allow you to break up your code into separate files. This
makes it easier to maintain a code-base. Modules rely on two main keywords:
export and import.
You can export variables or functions from any file. There are two types of exports: Named and Default.
You can create named exports two ways. In-line individually, or all at once at the bottom.
// person.js
export const name = "Mim";
export const age = 22;
// OR
const name = "Mim";
const age = 22;
export { name, age };
You can only have one default export in a file.
// message.js
const message = () => {
return "Hello World!";
};
export default message;
You can import modules into a file in two ways, based on if they are named exports or default exports.
import { name, age } from "./person.js";
import message from "./message.js";
To use modules in an HTML file, the <script> tag must have the
type="module" attribute.
<script type="module" src="main.js"></script>
file:// protocol (double-clicking a local HTML file)
cannot load modules. You must use a local server!
export to make code available to othersimport to bring in external code{ } when importing