class Student {
constructor(name, age) {
// runs automatically on "new"
this.name = name;
// attach data to the new instance
this.age = age;
} introduce() {
// shared method — lives on the prototype
return `Hi, I'm ${this.name}, ${this.age} 👋`;
}
}const s1 = new Student("Priya", 22);
// ✅ always with new
const s2 = new Student("Amit", 24);
console.log(s1.introduce());
// "Hi, I'm Priya, 22 👋"
console.log(s1.introduce === s2.introduce);
// true — one shared copy (Lesson 48!)JavaScript Classes: Building Objects with Modern Class Syntax
Bottom-Line Answer: JavaScript (JS) is a high-level, single-threaded, just-in-time (JIT) compiled programming language with first-class functions that conforms to the ECMAScript specification. It powers dynamic client-side web interactivity alongside HTML and CSS, and executes backend server runtimes via Node.js, Deno, and Bun.
What is JavaScript Classes? Core Concept & Direct Summary
How do you define ES6 classes, static methods, and constructor methods?
you saw the 1995 machinery underneath. Now it's time to master thecomplete modern class syntax— every feature, every keyword, every pattern — the way professional 2025 JavaScript actually writes classes.
Here's the complete toolkit at a glance — don't worry, we'll build each piece:
Constructor, methods, private fields, static members, getters, inheritance — by the end of this page, every line above will be second nature. Let's assemble the full blueprint. 🏗️
How Does JavaScript Classes Work? In-Depth Mechanics & Architecture
The Foundation: class, constructor, new 🧱
Quick refresher of the core (full story in Lesson 23):
The non-negotiable rules:
Class Fields: Properties Without the Constructor 🌱
Modern classes (2022+) let you declare fieldsdirectly in the class body— cleaner than packing everything into the constructor:
When to use fields vs constructor:
Arrow Function Fields: Thethis-Safe Methods 🏹
Remember Lesson 29's lost-thisbug? Class fields with arrow functions solve it permanently:
Regular methods losethiswhen borrowed; arrow fields never do. That's why UI components love this pattern.
Private Fields & Methods: The # Fortress 🔒
Lesson 23 introduced#fields; here's the complete privacy toolkit:
#= enforced by the language itself.Unlike the old_underscoreconvention (a polite request),#is a locked door — outside code physically cannot compile access to it. (Lesson 22's closure privacy achieves the same goal differently — closures hide data in scope;#hides it in syntax.)
Static Members: Belonging to the Class Itself 🏛️
Some things belong to theblueprint, not the instances — counters, factories, utilities. That'sstatic:
The mental model:instances are cookies 🍪 — statics are notes on the cookiecutteritself. You've used statics all along:Math.round(),Array.isArray(),Number.isNaN(),Object.keys()— all static methods on built-in classes!
Getters, Setters & Computed Names ✨
Lesson 31's guards work natively in class bodies (with#fields as their vaults):
Bonus: Computed Method Names
(Rare in beginner code — but you'll recognize it in libraries. And yes, Lesson 53's well-known symbols use exactly this bracket syntax!)
Inheritance Refresher: extends & super 🧬
Lesson 23 covered the concept; here's the complete syntax in one place:
The two faces ofsuper:
super(args)— in the constructor, calls the parent's constructor (mandatory first linebeforethis)super.method()— in methods, calls the parent's version of a method
🛠️ Mini Project: The Complete Class — Task Manager ✅
Every feature from this lesson in one realistic class:
Output:
Fields, privacy, statics, getters, find/filter — one class, the complete modern toolkit working together. 🏆
The Complete Class Syntax Card 🗂️
Common Beginner Mistakes (And Fixes)
Quick Self-Check: Lesson 55 Quiz 🧠
- What's the difference between a class field and a constructor assignment?
- What does
staticmean, and how do you call a static method? - What's the output?
- Why can't
acc.#balancebe accessed from outside the class — and what error does it produce? - When does an arrow-function class field beat a regular method?
(Answers: 1. Fields initialize with the same default for every instance and live in the class body; constructor assignments receive per-instance arguments — both end up on the instance · 2. Static members belong to the class itself, not instances — call them on the class:Counter.count,ClassName.method()· 3. 2 — each construction increments the one shared static counter · 4. # fields are hard-private, enforced by the language — outside access is a SyntaxError (the code won't even parse) · 5. When the function will be borrowed as a callback (event handlers, timers) — arrow fields keepthislocked to the instance, regular methods lose it)
JavaScript Decision Matrix & Syntax Comparison
Understanding the mechanical differences between variable declarations and execution environments is critical for writing performant, bug-free modern JavaScript.
var vs let vs const| Feature / Property | var (Legacy ES5) | let (Modern ES6+) | const (Modern ES6+) |
|---|---|---|---|
| Scope | Function Scope | Block Scope ({}) | Block Scope ({}) |
| Hoisting Behavior | Hoisted (initialized as undefined) | Hoisted (Temporal Dead Zone - ReferenceError) | Hoisted (Temporal Dead Zone - ReferenceError) |
| Reassignable | ✓ Yes | ✓ Yes | ✗ No (Immutable binding) |
| Redeclarable in Same Scope | ✓ Yes (Error-prone) | ✗ No (SyntaxError) | ✗ No (SyntaxError) |
| Window Object Binding | Creates property on window | Does not attach to window | Does not attach to window |
| Dimension | Client-Side (Web Browser) | Server-Side (Node.js / Deno) |
|---|---|---|
| Global Object | window, document, navigator | global, process, globalThis |
| DOM & UI Manipulation | ✓ Full access (document.querySelector) | ✗ No DOM (Headless / Non-UI) |
| File System & OS Access | ✗ Blocked (Sandboxed security) | ✓ Full access (fs, os, child_process) |
| Primary Focus | Interactive UI, animations, client state | REST APIs, microservices, databases, I/O streaming |
Runnable JavaScript Code Example & Practical Implementation
class Car {
constructor(brand) {
this.brand = brand;
} drive() {
return `${this.brand} is driving`;
} static isVehicle(obj) {
return obj instanceof Car;
}
}const myCar = new Car('Tesla');
console.log(myCar.drive());
// 'Tesla is driving'
console.log(Car.isVehicle(myCar));
// trueFrequently Asked Questions & Technical Interview Insights
Technical Interview & Viva Questions
Frequently asked questions for frontend & full-stack rolesQ: What is the purpose of private class fields (#field) in ES2022?
Related Tools, Tutorials & Authoritative References
Recommended Interactive Tools
- Online JavaScript Editor & IDE — Browser-based JavaScript workspace with live console and DOM inspector.
- C Programming Tutorial — Master memory allocation, pointers, and systems programming fundamentals.
- C++ Programming Tutorial — Explore Object-Oriented Programming, templates, and high-performance STL.
- Online Code Editors & Compilers — Complete development tool suite for 20+ programming languages.
Authoritative Standards & Citations
- ECMA-262 ECMAScript Language Specification — The official international JavaScript language specification maintained by TC39.
- MDN Web Docs (Mozilla Developer Network) — Authoritative documentation and API guide for JavaScript and Web APIs.
- Google V8 JavaScript Engine — Open-source high-performance JavaScript and WebAssembly engine documentation.
- Node.js Official Documentation — Server-side JavaScript runtime API specification and reference.