Async & Modern JS · 9 min read · Beginner
ES Modules & Modern Classes
Organize growing codebases cleanly with native import/export ES Modules and encapsulate state and methods with ES6 Classes and private #fields.
What you'll learn
- Share functions and data between files using named and default export / import
- Define clean classes with constructor, methods, and private #fields
- Know when a plain object/function is simpler than a class
ES Modules (import / export)
Every modern browser natively supports <script type="module">, letting you split your app into small, focused files:
// math.js
export const clamp = (n, min, max) => Math.min(Math.max(n, min), max);
export default function double(n) { return n * 2; }
// app.js
import double, { clamp } from './math.js';
Modern Classes & True Private Fields (#)
Prefixing a property with # makes it genuinely private, external code cannot read or mutate it directly:
class ProgressTracker {
#completed = new Set();
complete(slug) { this.#completed.add(slug); }
get count() { return this.#completed.size; }
}
Fill-in-the-Blank Exercise
Named Export & Private Field
Export a helper function by name and declare a private #score field in a class.
function formatScore(n) {
return n + '%';
}
class QuizState {
score = 0;
}Try It Yourself
ES6 Class with Private #completed Set
Live Result
Console Output
No console output yet. Try calling console.log(...) in JS.
Check Your Understanding
Score: 0 / 1
1. What does prefixing a JavaScript class field with `#` (like `#token`) do?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.