What is an operator in RxJS?
An operator in RxJS is a function that transforms, filters, or combines a data stream (Observable).
A hands-on example:
You're listening to a stream of numbers: 1, 2, 3, 4
Want to keep only the even ones → use the filter operator
Want to multiply by 10 → use map
ts
import { of } from 'rxjs';
import { filter, map } from 'rxjs/operators';
of(1, 2, 3, 4).pipe(
filter(n => n % 2 === 0),
map(n => n * 10)
).subscribe(val => console.log(val));Outputs: 20, 40
Why operators are needed:
- process data (
map,filter,tap) - manage time (
debounceTime,delay,throttleTime) - switch streams (
switchMap,mergeMap,concatMap) - stop (
take,takeUntil) - combine (
combineLatest,withLatestFrom)
How they work:
Operators are used inside .pipe(...), which is like a processing chain:
ts
observable.pipe(
operator1,
operator2,
...
)Conclusion:
An operator in RxJS is a tool that transforms a data stream. With it, you're saying: "don't just listen to everything, process it like this." Without operators, RxJS would just be a listener. With them, it's like a stream processor.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.