Advanced Topics

JavaScript has several advanced features. You do not need them for everyday programming, but they are useful in specific situations. They let you do metaprogramming, custom iteration, pattern matching, and more. You will run into them in libraries and frameworks, and when you work on specialized problems.

// A generator function that yields Fibonacci numbers
function* fibonacci() {
  let [prev, curr] = [0, 1];
  while (true) {
    yield curr;
    [prev, curr] = [curr, prev + curr];
  }
}

const fib = fibonacci();
console.log(fib.next().value);  // 1
console.log(fib.next().value);  // 1
console.log(fib.next().value);  // 2
console.log(fib.next().value);  // 3
console.log(fib.next().value);  // 5

You do not need to master these right away, but knowing how they work will make you better at JavaScript.

When to Use These Features

  • Symbol: Creating unique property keys, implementing well-known behaviors
  • Iterators and Generators: Custom iteration, lazy evaluation, async data streams
  • Regular Expressions: Pattern matching, text parsing, validation
  • Proxy and Reflect: Metaprogramming, validation, logging, virtual objects
  • WeakMap and WeakSet: Private data, caching, object tracking without memory leaks

Learning Outcomes

  • Use symbols, including well-known symbols, to create unique property keys and customize object behavior
  • Implement the iteration protocol with custom iterables, generators, and async generators
  • Write and apply regular expressions for pattern matching, validation, and text processing
  • Use Proxy and Reflect to intercept and customize object operations for metaprogramming
  • Use WeakMap and WeakSet to manage object references without preventing garbage collection

Sections

  1. Symbol
  2. Well-Known Symbols
  3. Iterators
  4. Generators
  5. Async Generators and Practical Patterns
  6. Regular Expressions
  7. Regex Groups and String Methods
  8. Regex Patterns and Pitfalls
  9. Proxy and Reflect
  10. Reflect API and Proxy Patterns
  11. Advanced Proxy Patterns
  12. WeakMap and WeakSet
  13. Practice Questions