A comprehensive collection of JavaScript algorithms, data structures, and fundamental JavaScript concepts with detailed explanations and examples.
algo/
βββ README.md
βββ LICENSE
βββ algo/ # Additional algorithms subdirectory
β βββ deep-clone.js # Deep cloning algorithm
β βββ flatten-array.js # Array flattening algorithm
β βββ group-anagram.js # Group anagrams algorithm
βββ js-concepts/ # JavaScript fundamentals and concepts
βββ 0-simple-operation.js
βββ 1-array-using-plus.js
βββ 2-lexical-arrow-vs-function.js
βββ 3-loop-var-vs-let.js
βββ 4-closure.js
βββ 5-scope-functional-vs-block.js
βββ 6-null-is-object.js
βββ 7-decimal-value.js
βββ 8-compare-object.js
βββ 9-non-null-if-condition.js
βββ 10-variable-reassignment.js
βββ 11-reference-assignment.js
βββ 12-array-value-assignment.js
βββ 13-hoisted-value.js
βββ 14-NaN-value.js
βββ 15-add-increment.js
βββ 16-number-string-operation.js
βββ 17-multiple-string-number-operation.js
βββ 18-function-arg-reassignment.js
βββ 19-instanceof.js
βββ 20-object-math-operation.js
βββ 21-typeof-class.js
βββ 22-array-reassignment.js
βββ 23-bind.js
βββ 24-prototype-object.js
βββ 25-apply-call-function.js
βββ 26-promises.js
βββ 27-classes.js
βββ 28-class-private-static.js
βββ 29-loop-function.js
βββ 30-sort-array.js
βββ 31-destructuring.js
βββ 32-slice-splice.js
βββ 33-event-loop.js
Files: algo/deep-clone.js
A recursive deep cloning algorithm that creates a complete copy of nested objects and arrays without sharing references.
const original = {
name: "John",
age: 30,
skills: ["JS", "React"],
address: {
city: "Delhi",
coords: { lat: 28.6, lng: 77.2 },
},
};
const clone = deepClone(original);
// Modifications to clone don't affect originalKey Features:
- Handles nested objects and arrays
- Preserves primitive values
- Creates completely independent copies
- Recursive implementation
Files: algo/flatten-array.js
Recursive algorithm to flatten deeply nested arrays into a single-level array.
const nested = [1, [2, [3, 4], 5]];
const flattened = flattenArray(nested);
// Result: [1, 2, 3, 4, 5]Features:
- Handles arbitrary nesting depth
- Preserves element order
- Works with mixed data types
File: algo/group-anagram.js
Algorithm to group strings that are anagrams of each other using a hash map approach.
const input = ["eat", "tea", "tan", "ate", "nat", "bat"];
const result = groupAnagrams(input);
// Result: [["eat","tea","ate"], ["tan","nat"], ["bat"]]Key Features:
- Uses Map data structure for efficient grouping
- Sorts characters to create unique keys
- Handles multiple anagram groups
- Time complexity: O(n * k log k) where n is number of strings, k is max string length
Algorithm Approach:
- Sort each string's characters to create a unique key
- Use Map to group strings with the same sorted key
- Return grouped arrays
- Loose equality (
==) vs Strict equality (===) - Type coercion examples
- Best practices for comparisons
- Array concatenation with
+operator - String conversion behavior
- Proper array concatenation methods
varvsletvsconst- Block scope vs function scope
- Temporal dead zone (TDZ)
- Arrow functions vs regular functions
- Lexical scoping
- Closure creation and usage
- Practical closure examples
- Function scope vs block scope
- Variable accessibility rules
typeofoperator behaviornullandundefineddifferencesNaNproperties and detection- Class type checking
- Floating-point precision
- Decimal arithmetic behavior
- Object comparison
- Reference vs value comparison
- Object mathematical operations
- Truthy/falsy values
- Conditional evaluation
- Null checking patterns
- Variable reassignment behavior
- Reference vs value assignment
- Object and array reference behavior
- Shallow vs deep copying
- Array element assignment patterns
- Reference sharing in arrays
- Variable hoisting
- Function hoisting
- Temporal dead zone
15. Operators and Operations (15-add-increment.js, 16-number-string-operation.js, 17-multiple-string-number-operation.js)
- Increment/decrement operators
- Number-string operations
- Type coercion in operations
- Function argument reassignment
bind(),apply(),call()methods- Function context and
this
instanceofoperator- Constructor checking
- Inheritance verification
- Array reassignment patterns
- Reference behavior in arrays
- Prototype chain
- Object inheritance
- Prototype methods
- Promise fundamentals
- Promise chaining
async/awaitsyntax- Promise methods:
Promise.all()Promise.race()Promise.any()Promise.allSettled()
- Class syntax and inheritance
- Method overriding
super()keyword usage- Constructor behavior
- Private fields (
#privateField) - Static methods and properties
- Class inheritance patterns
- Instance vs static access
map(),filter(),reduce()functions- Array transformation patterns
- Functional programming concepts
sort()method behavior- Lexicographic vs numeric sorting
- Array mutation considerations
- Object destructuring
- Nested destructuring
- Default values
- Fallback patterns
slice()vssplice()methods- Mutating vs non-mutating operations
- Array safety considerations
- JavaScript execution model
- Microtasks vs macrotasks
async/awaitexecution order- Promise vs
setTimeouttiming
-
Clone the repository:
git clone <repository-url> cd algo
-
Run individual files:
node deep-clone.js node flatten-array.js node js-concepts/0-simple-operation.js
-
Explore concepts:
- Each file contains detailed comments explaining the concept
- Run files to see output and understand behavior
- Modify examples to experiment with different scenarios
- Start with
0-simple-operation.jsfor basic equality concepts - Move to
1-array-using-plus.jsfor array operations - Study
3-loop-var-vs-let.jsfor variable scoping - Explore
4-closure.jsfor closure concepts
- Study
6-null-is-object.jsand14-NaN-value.jsfor type system - Practice with
8-compare-object.jsfor object operations - Learn
13-hoisted-value.jsfor hoisting behavior - Master
16-number-string-operation.jsfor type coercion
- Master
26-promises.jsfor asynchronous programming - Deep dive into
24-prototype-object.jsfor inheritance - Practice with
25-apply-call-function.jsfor function context - Study
27-classes.jsand28-class-private-static.jsfor modern classes - Explore
33-event-loop.jsfor JavaScript execution model
- Type Safety: Always use strict equality (
===) and understand type coercion - Scope Management: Prefer
letandconstovervarfor better scoping - Object References: Be careful with object and array assignments
- Asynchronous Patterns: Master promises and async/await for modern JavaScript
- Memory Management: Understand closures and their memory implications
- Array Safety: Use non-mutating methods when possible
- Class Design: Leverage private fields and static methods for better encapsulation
- Modern JavaScript: Classes, private fields, static methods
- Functional Programming: Map, filter, reduce patterns
- Asynchronous Programming: Promises, async/await, event loop
- Object-Oriented Programming: Inheritance, prototypes, method overriding
- Array Manipulation: Destructuring, slicing, sorting, flattening
Feel free to contribute by:
- Adding new JavaScript concepts
- Improving existing examples
- Adding more detailed explanations
- Creating additional algorithms
- Suggesting new topics to cover
This project is open source and available under the MIT License.
Happy Learning! π