Quiz Entry - updated: 2026.06.20
How do ES modules (import/export) work in JavaScript?
Each file is its own module: export exposes values from it and import pulls them into another file.
Exporting (hello.js):
const value = { text: 'hello world' };
export default value;
Importing (main.js):
import hello from './hello.js';
// 'hello world'
console.log(hello.text);
Key concepts:
- Each file is a module with its own scope
export defaultexports one main valueimport name from './file.js'imports the default export- The imported name can be anything you choose
Named exports (alternative):
// math.js
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;
// main.js
import { add, subtract } from './math.js';