Advanced JavaScript
Learn Advanced JavaScript with small working examples of prototypes, closures, generators, proxies, workers, and the event loop. Follow each result step by step.
12 programs · open one, then the next
Start with Prototypes- 01PrototypesAdvancedShare a greeting method in JavaScript with Student.prototype and new Student. Object.hasOwn separates own names from the method shared by both students.Open example →jsfunction Student(name) { this.name = name; } Student.prototype.greet = function () { return 'Hello, ' + this.name; }; const asha = new Student('Asha'); const kabir = new Student('Kabir');
- 02InheritanceAdvancedExtend a library item in JavaScript with class Book, extends, and super. Override describe to add an author; call the parent constructor before using this.Open example →jsclass LibraryItem { constructor(title) { this.title = title; } describe() { return 'Item: ' + this.title; } }
- 03Closures AdvancedAdvancedKeep separate visitor counts in JavaScript with closures returned by createCounter. Methods share private local state; the demo still allows negative counts.Open example →jsfunction createCounter(label) { let count = 0; return { increment() { count++; return label + ': ' + count; },
- 04Currying AdvancedAdvancedReuse price and quantity in JavaScript with curry3 and chained single-argument calls. Add delivery in the final call; the helper requires exactly three inputs.Open example →js// This helper is for exactly three required arguments. function curry3(fn) { return a => b => c => fn(a, b, c); }
- 05Memoization AdvancedAdvancedCache square results in JavaScript with a closure and Map.has, so a cached zero is reused too. This cheap calculation teaches the pattern, not a measured speedup.Open example →jsfunction memoizeSquare() { const cache = new Map(); return function (number) { if (!Number.isFinite(number)) throw new Error('Use a finite number');
- 06Generators AdvancedAdvancedYield Fibonacci values in JavaScript with function* and a term limit. for...of consumes the generator; create a fresh one to restart an exhausted sequence.Open example →jsfunction* fibonacci(limit) { let a = 0, b = 1; for (let count = 0; count < limit; count++) { yield a;
- 07Iterators AdvancedAdvancedIssue three tickets in JavaScript with Symbol.iterator and next results containing value and done. Each new loop gets a fresh cursor rather than shared progress.Open example →jsconst tickets = { [Symbol.iterator]() { let number = 1; return {
- 08ProxiesAdvancedValidate marks in JavaScript with Proxy get/set traps and Reflect. Reject integers outside 0–20; writes to the target object directly bypass the traps.Open example →jsconst student = { name: 'Asha', marks: 18 }; const record = new Proxy(student, { get(target, property, receiver) { if (!(property in target)) return 'Not recorded';
- 09Reflect APIAdvancedRead, write, and delete properties in JavaScript with Reflect methods. Refused changes to a fixed property return false; invalid targets can still throw errors.Open example →jsconst desk = { room: 'B12', seats: 4 }; console.log('Room:', Reflect.get(desk, 'room')); console.log('Write seats:', Reflect.set(desk, 'seats', 6)); console.log('Has seats:', Reflect.has(desk, 'seats'));
- 10Web Workers AdvancedAdvancedTransfer an ArrayBuffer in JavaScript to a web worker and return a total with a job ID. The sender loses access to the buffer; its byteLength becomes zero.Open example →jsconst source = ` self.onmessage = event => { const values = new Uint32Array(event.data.buffer); let total = 0;
- 11Event LoopAdvancedTrace the event loop in JavaScript: synchronous logs run first, then a promise microtask, then a timer. A zero delay does not mean immediate execution.Open example →jsconsole.log('Start revision'); setTimeout(() => console.log('Timer reminder'), 0); Promise.resolve().then(() => console.log('Saved-note microtask')); console.log('Finish current script');
- 12Microtasks and MacrotasksAdvancedCompare queueMicrotask, promise reactions, and timers in JavaScript. A nested microtask joins the queue's end; all queued microtasks run before the timer.Open example →jsconsole.log('A: script starts'); setTimeout(() => console.log('E: timer task'), 0); queueMicrotask(() => { console.log('C: first microtask');