Curriculum
Concept 69 // Module 07: Collections, Iteration & Modules
ES Modules: import, export, Module Scope & Import Attributes
Module 07: Collections, Iteration & Modules•Concept 69 of 100• 5 min read
ES Modules: import, export, Module Scope & Import Attributes
**ES Modules (ESM)** are the official standard for organizing and sharing JavaScript code. They allow you to break your application into smaller, reusable files and manage dependencies explicitly.
The Core Concept
Syntax Reference
// file: math.js
export const add = (a, b) => a + b;
export default function multiply(a, b) { ... }
// file: app.js
import multiply, { add } from './math.js';
Live Interactive Studio
Default exports are imported without curly braces. Named exports must use curly braces `{}` and match the name exactly.
JavaScript Editor
Loading...
Console Output
Waiting for output...
Key Code Patterns in Action
01
Named vs Default Exports
// 1. Named vs Default Exports
// helpers.js
export const config = { port: 3000 };
export function log(msg) { console.log(msg); }
export default class App {}
// main.js
import App, { config, log } from './helpers.js';Default exports are imported without curly braces. Named exports must use curly braces `{}` and match the name exactly.
02
Renaming Imports
// 2. Renaming Imports
import { longFunctionName as log } from './utils.js';
log("Hello");The `as` keyword allows you to rename an import to avoid name collisions in your current file.
03
Re-exporting (Aggregator Pattern)
// 3. Re-exporting (Aggregator Pattern)
// index.js
export { default as User } from './User.js';
export { default as Product } from './Product.js';
export * from './constants.js';You can create an 'entry point' file that collects exports from many files, allowing users to import everything from a single folder.
04
1. Core Usage: ES Modules
// Core syntax and usage for ES Modules: import, export, Module Scope & Import Attributes
const feature = 'es-modules-import-export-scope-import-attributes';
console.log('Concept 69:', feature);
console.log('Active:', true);Illustrates the fundamental invocation and syntax structure for ES Modules: import, export, Module Scope & Import Attributes.
05
2. Defensive Handling & Edge Case
// Edge-case defensive handling
function handleSafe(input) {
if (input === null || input === undefined) return 'safe-fallback';
return String(input).trim();
}
console.log(handleSafe('es-modules-import-export-scope-import-attributes'));
console.log(handleSafe(null));Shows how to write defensive, safe JavaScript code that handles nullish or unexpected inputs when working with ES Modules: import, export, Module Scope & Import Attributes.
06
3. Clean Architecture & Production Pattern
// Production-grade pattern for ES Modules: import, export, Module Scope & Import Attributes
const settings = Object.freeze({
id: 'es-modules-import-export-scope-import-attributes',
ready: true,
timestamp: Date.now()
});
console.log('Settings initialized:', settings.id, settings.ready);Demonstrates clean, immutable production code avoiding unintended state mutations.
Real-World Engineering Takeaways
Code Splitting: Loading only the code needed for a specific page.
Dependency Management: Clearly seeing what each file depends on.
Namespacing: Avoiding global variable collisions.
Maintainability: Keeping files small and focused on a single task.
Under the Hood: Engine & Memory Mechanics
How JavaScript Executes This
### **Module Loading Lifecycle**
**1. Static Analysis:**
- Imports are analyzed **before** any code runs.
- This allows tools like Vite to "tree-shake" (remove unused code) from the final bundle, saving **RAM** and download time.
**2. Module Registry:**
- The browser maintains a registry of loaded modules in the **Heap**.
- Each module is executed **exactly once**, no matter how many times it's imported.
**3. Singleton Behavior:**
- If you export an object, every file that imports it gets a reference to the **same object in memory**.
**4. Performance:**
- Modules run in **Strict Mode** by default.
- Deeply nested imports can increase "Main Thread Blocking" time during initial page load as the browser fetches and parses each file.
Quick Knowledge Check
Test your understanding on these 10 questions (one by one):
Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model
What is the core purpose of ES Modules: import, export, Module Scope & Import Attributes in JavaScript?
// Concept 69: ES Modules: import, export, Module Scope & Import Attributes
// Understanding the core mental model1 / 10