A curated collection of JavaScript fundamentals implemented from scratch in TypeScript. Each topic includes a working implementation with test cases — built for interview prep and deep understanding.
Repository: github.com/RohanGau/js-fundamentals
Table of Contents
Array Polyfills
1. Array.prototype.map
Implement Array.prototype.map from scratch. Handles sparse arrays, thisArg, and callback validation — matching native spec behavior.
Key concepts: Prototype extension, Object(this), >>> unsigned right shift, call with thisArg
2. Array.prototype.filter
Implement Array.prototype.filter from scratch. Returns a new array with elements that pass the predicate test.
Key concepts: Prototype extension, sparse array handling, immutability
3. Array.prototype.reduce
Implement Array.prototype.reduce from scratch. Accumulates array values into a single result using a callback.
Key concepts: Accumulator pattern, initial value handling, empty array edge case
Promise Polyfills
4. Promise.all
Implement Promise.all from scratch. Resolves when all promises fulfill (preserving order) or rejects on the first failure.
Key concepts: Promise coordination, order preservation, short-circuit rejection
5. Promise.any
Implement Promise.any from scratch. Returns the first fulfilled promise or throws AggregateError when all reject.
Key concepts: First-success pattern, AggregateError, inverse of Promise.all
Utility Functions
6. classNames
Build a classNames/clsx utility from scratch. Merges CSS class names from strings, objects, arrays, and functions — with truthy/falsy toggling.
Key concepts: Recursive type handling, conditional class toggling, variadic arguments
7. Flatten
Flatten a nested array of arbitrary depth without mutating the original. Uses an explicit stack to avoid recursion depth limits.
Key concepts: Stack-based iteration vs recursion, depth handling, immutability
Core Patterns
8. Debounce
Implement debounce — delays function execution until after a specified wait period has elapsed since the last call.
Key concepts: setTimeout/clearTimeout, this context preservation, closures
9. Throttle
Implement throttle — limits function execution to at most once per specified interval.
Key concepts: Rate limiting, leading/trailing invocation, timing control
10. Curry
Implement function currying — transforms a multi-argument function into a sequence of single-argument functions.
Key concepts: Closures, Function.length (arity), argument accumulation, partial application
11. Deep Clone
Deep clone JavaScript values while preserving prototypes, symbol keys, and handling circular references.
Key concepts: Recursion, WeakMap for circular refs, Reflect.ownKeys, prototype preservation, prototype pollution defense
12. Deep Equal
Implement deep equality comparison for primitives, arrays, and objects — handling type mismatches and nested structures.
Key concepts: Recursive comparison, typeof limitations with arrays, structural equality vs referential equality
13. Data Merge
Merge an array of session objects by user ID — summing durations and deduplicating equipment lists.
Key concepts: Map for grouping, Set for deduplication, data transformation
Advanced Concepts
14. Generators
Generator functions and iteration patterns — yield, delegation with yield*, and infinite sequences with idMaker.
Key concepts: Generator protocol, yield/yield*, lazy evaluation, iterator protocol
15. Promise.all Demo
Practical demo of coordinating multiple async operations with Promise.all.
Key concepts: Async orchestration, parallel execution, error handling
How to Run
# Clone the repo
git clone https://github.com/RohanGau/js-fundamentals.git
cd js-fundamentals
# Install dependencies
npm install
# Run all tests
npm test
# Watch mode (re-runs on save)
npm run test:watch
# Run in browser
npm run serve
Tech Stack
- TypeScript — Strict mode, full type safety
- Vitest — Fast unit testing
- ESM — Modern module system
- Zero dependencies — No frameworks
Want to Contribute?
Feel free to open a PR to add new topics, improve existing implementations, or add more test cases.
Repository: github.com/RohanGau/js-fundamentals
Built by Rohan for learning JavaScript fundamentals the right way.