| Event Bubbling & Delegation | let, const, arrow functions | |
JavaScript Modules: Import & Export |
JavaScript modules allow developers to split code into reusable pieces across multiple files. This improves maintainability, readability, and scalability of applications. Modules are supported natively in modern browsers and JavaScript environments.
You can export variables, functions, or classes from a module so they can be used in other files.
// utils.js export const add = (a, b) => a + b; export const subtract = (a, b) => a - b;
// message.js const greeting = "Hello, world!"; export default greeting;
Use the import statement to bring in exported code from another module.
// app.js
import { add, subtract } from './utils.js';
console.log(add(2, 3)); // 5
// main.js import greeting from './message.js'; console.log(greeting); // Hello, world!
To use modules in the browser, set type="module" in your script tag:
<script type="module" src="main.js"></script>
With the introduction of ECMAScript 2015 (ES6), JavaScript gained a standardized module system for organizing code across multiple files. This system uses the import and export statements to manage dependencies, control scope, and enable code reuse.
Each JavaScript file can be a module, and its top-level variables and functions are scoped to that file by default. This prevents naming conflicts and keeps the global scope clean. To use a function or variable from one module in another, it must be explicitly exported and then imported.
The export statement makes a variable, function, class, or object available for use in other modules. There are two primary types of exports:
You can have multiple named exports in a single module. The importing module must use the exact same name to access the exported value.
./math.js
// Export a constant
export const PI = 3.14159;
// Export a function
export function add(a, b) {
return a + b;
}
// Export multiple items at once
const subtract = (a, b) => a - b;
const multiply = (a, b) => a * b;
export { subtract, multiply };
A module can have only one default export. This is useful for when a module's main purpose is to provide a single value, such as a class or a function. The importing module can use any name for a default export.
./user.js
// Export a default class
export default class User {
constructor(name) {
this.name = name;
}
}
Important: A module can have both a default export and named exports.
The import statement is used in a module to bring in functionality that has been exported from another module.
To import named exports, you must use the exact names and wrap them in curly braces {}.
./app.js
import { PI, add, multiply } from './math.js';
console.log(add(PI, 2)); // 5.14159
console.log(multiply(4, 5)); // 20
To import a default export, you do not use curly braces. You can name the import anything you like.
./app.js
import MyUserClass from './user.js';
const user = new MyUserClass('Alice');
console.log(user.name); // Alice
You can import all the named exports from a module as a single namespace object. This is useful for avoiding naming conflicts.
./app.js
import * as math from './math.js'; console.log(math.PI); // 3.14159 console.log(math.add(4, 5)); // 9
You can import both a default export and named exports in a single line.
./app.js
import User, { PI, add } from './user_and_math.js';
// Assuming user_and_math.js exports both a default and named exports
To use ES6 modules in a web browser, you must include the type="module" attribute in your <script> tag.
<!DOCTYPE html>
<html>
<body>
<script type="module" src="./app.js"></script>
</body>
</html>
import { add as myAdd } from './math.js';import * as utils from './utils.js') keeps your code clean and prevents polluting the local namespace.import() expression, which returns a Promise. | Event Bubbling & Delegation | let, const, arrow functions | |