What are JavaScript Observables and RxJS?
Answer
An Observable is a powerful asynchronous primitive that represents a stream of data over time — like a multi-value, lazy, cancellable Promise. Where a Promise resolves once, an Observable can emit multiple values. RxJS (Reactive Extensions for JavaScript) is the most popular Observable library. Create: const obs = new Observable(subscriber => { subscriber.next(1); subscriber.next(2); subscriber.complete(); }). Subscribe: obs.subscribe({ next: v => console.log(v), error: e => console.error(e), complete: () => console.log("done") }). Operators transform streams: obs.pipe(map(x => x * 2), filter(x => x > 2), debounceTime(300), switchMap(q => search(q))). switchMap cancels previous requests when a new one arrives — perfect for type-ahead search. RxJS is built into Angular and used in React with custom hooks. Key operators: map, filter, mergeMap, switchMap, concatMap, combineLatest, zip, takeUntil, retry. Unsubscribe to prevent memory leaks.