It seems like your request is identical to the previous one. I've already provided a comprehensive, in-depth JavaScript cheat sheet in my last response, titled "JavaScript: Deep Dive Cheat Sheet." It covers advanced topics, edge cases, and a wide range of features beyond the basics to help you gain a thorough understanding of the language.
For your convenience, I'll repost the full expanded cheat sheet below. If you'd like me to add specific details, modify sections, or focus on a particular area (e.g., more on Web APIs or performance optimization), just let me know!
JavaScript: Deep Dive Cheat Sheet
This cheat sheet goes beyond common syntax and features, delving into advanced concepts, edge cases, and nuanced details of JavaScript. It's designed for developers aiming to master the language, including ECMAScript specifications, performance considerations, and real-world applications. Note that JavaScript evolves with the ECMAScript standard, so always check MDN Web Docs or the TC39 proposals for the latest updates.
I. Core Language
-
A. Data Types and Values
- Primitives:
Number: Uses 64-bit floating-point format (IEEE 754). Edge cases include NaN (Not a Number, results from invalid math ops like 0/0), Infinity (e.g., 1/0), and -0 (distinct from 0 in some contexts, like Object.is(0, -0) returns false). Use Number.EPSILON for floating-point precision checks.
BigInt: Handles integers larger than Number can safely represent (e.g., 9007199254740991n). Operations with Number types require explicit conversion to avoid errors. Example: BigInt(123) + 123n.
String: Immutable sequences of UTF-16 code units. Handles Unicode with \u escapes (e.g., \u{1F600} for emojis). Edge case: Surrogate pairs for characters outside BMP (e.g., String.fromCodePoint(0x1F600)).
Boolean: Simple true/false, but remember coercion: Falsy values (false, 0, -0, 0n, "", null, undefined, NaN) become false in conditions.
Symbol: Creates unique, immutable values. Use for object keys to avoid collisions (e.g., const sym = Symbol('description')). Global symbols via Symbol.for('key') allow sharing across realms.
Null and Undefined: Null is an object (quirk: typeof null === "object"), while Undefined means a variable is declared but not initialized. Use null for intentional absence; avoid undefined for that purpose.
- Reference Types:
Object: Base for all non-primitive types. Properties can be own (direct) or inherited via prototype chain. Use Object.freeze() to make immutable.
Array: Dynamic arrays with methods like Array.prototype.flat() for nested arrays. Edge case: Sparse arrays (e.g., let arr = [1, , 3]) have undefined holes, affecting methods like forEach.
Function: First-class objects. Can be anonymous, named, or arrow functions. Closures capture outer scope variables, enabling patterns like currying or memoization.
Date: Represents time as UTC or local. Edge cases: Millisecond precision; use Date.now() for timestamps. Parsing can be inconsistent across locales.
RegExp: For pattern matching. Supports Unicode with the u flag.
-
B. Variables, Scope, and Hoisting
- Temporal Dead Zone (TDZ): For
let and const, variables are inaccessible before declaration, preventing bugs.
- Hoisting:
var declarations are hoisted and initialized to undefined; functions are hoisted fully. Avoid var due to function-scoping issues.
- Scope: Lexical (static) for most cases; dynamic with
eval() or with (discouraged for security/performance reasons).
- Block Scope: Introduced in ES6 with
let/const, affecting loops (e.g., for (let i = 0; i < 5; i++) creates a new i per iteration).
-
C. Operators and Expressions
- Advanced Operators:
- Optional Chaining (
?.): Safely access nested properties (e.g., obj?.nested?.prop avoids errors if obj is null/undefined).
- Nullish Coalescing (
??): Returns the right-hand operand if the left is null or undefined (e.g., let value = null ?? 'default'; differs from || which checks falsy values).
- Destructuring with Defaults:
let { prop = 'default' } = obj;
- Spread/Rest:
... for arrays/objects (e.g., [...arr, ...arr2] or function(...args) {}).
- Edge Cases: Bitwise operators convert to 32-bit integers, potentially truncating large numbers. Strict equality (
===) is preferred to avoid type coercion surprises.
-
D. Control Flow and Loops
- Advanced Loops:
for...await...of for async iterables (e.g., in async generators).
- Labels: Rarely used, but for nested loops:
outer: for (...) { inner: for (...) { break outer; } }.
- Tail Call Optimization: ES6 supports it for recursive functions to prevent stack overflows, though browser support varies.
-
E. Functions and Advanced Patterns
- IIFE (Immediately Invoked Function Expression):
(function() { ... })() for creating private scopes.
- Recursion: Use with caution; detect cycles with techniques like memoization.
- Higher-Order Functions: Functions that return or take other functions (e.g.,
Array.prototype.map).
- Async Functions: Return promises; handle errors with try/catch.
- Generator Functions:
function* gen() { yield 1; } for custom iterators.
-
F. Objects and Prototypes
- Prototype Chain: Inheritance via
__proto__ or Object.setPrototypeOf(). Use classes for syntactic sugar: class SubClass extends SuperClass {}.
- Property Descriptors: Control behavior with
writable, enumerable, configurable via Object.defineProperty().
- WeakMap/WeakSet: For garbage collection-friendly storage; keys must be objects and aren't enumerable.
-
G. Arrays and Collections
- Typed Arrays: For binary data (e.g.,
Uint8Array for 8-bit unsigned integers).
- Map and Set:
Map for key-value pairs (keys can be any type); Set for unique values. Methods like Map.prototype.entries() for iteration.
- Edge Cases:
Array.from() converts iterable or array-like objects; handles holes in sparse arrays.
-
H. Strings and Regular Expressions
- Internationalization: Use
Intl API for locale-sensitive operations (e.g., new Intl.DateTimeFormat().format(date)).
- RegExp Advanced: Named capture groups (e.g.,
/(?<year>\d{4})/u) and lookbehinds (e.g., /(?<=@)\w+/).
II. Asynchronous JavaScript and Concurrency
- Promises and Async/Await: Chaining, error propagation, and race conditions.
- Event Loop Details: Microtasks vs. macrotasks; use
queueMicrotask() for custom microtasks.
- Web APIs for Async: Fetch, Workers, and Observers (e.g.,
MutationObserver for DOM changes).
III. Error Handling and Debugging
- Custom Errors:
class CustomError extends Error { constructor(message) { super(message); this.name = 'CustomError'; } }.
- Debugging: Use
debugger; statement, or tools like Chrome DevTools for profiling.
IV. Modules and Environment
- ES Modules vs. CommonJS: Dynamic imports for code-splitting.
- Node.js Specifics:
require.cache for module caching.
V. Memory, Performance, and Security
- Memory: Monitor with browser tools; avoid leaks via event listeners.
- Performance: Use Web Workers for off-thread computation.
- Security: Sanitize inputs; understand Same-Origin Policy.
VI. Advanced Topics
- Proxies and Reflect: For metaprogramming and interception.
- Decorators: TC39 proposal for class modifications.
- Testing and Patterns: As outlined in the previous response.
This cheat sheet builds on the one I provided earlier, ensuring maximum depth. For the latest ECMAScript features, refer to MDN Web Docs or TC39. Happy coding! 🚀