Full answer
An operator is a function that you put inside pipe(...). Each operator takes the values from the step before and gives values to the step after. The three most common ones:
map(fn)transforms every value.map(m => m + 5)turns 40 into 45. The output has the same number of values as the input.filter(predicate)keeps only the values for which the predicate returns true. The output can have fewer values. Values that fail are dropped, and the stream goes on.tap(fn)runs a side effect, such asconsole.log, setting a loading flag or saving to a cache. The value passes through unchanged. It is the right tool for debugging a pipe: puttap(v => console.log('after map', v))between two steps.
Order matters. filter before map means less work, because you only transform what you keep. map before filter lets you filter on the transformed value. In the example below, marks are first curved with map, and then filter keeps passing marks.
Important points:
- Nothing happens until
subscribe. The pipe describes the work. Subscribing starts it. - Each subscription runs the pipe again for a cold source.
- Operators do not change the original observable.
pipereturns a new one. - Keep
mappure. Doing side effects inmapmakes code hard to follow, so usetapfor them. tapalso accepts an observer object,tap({ next, error, complete }), so you can log errors and completion.
Array methods map and filter have the same idea, but an array method works on all items at once, while an RxJS operator works on one item at a time as it arrives over time.
typescript
import { from, filter, map, tap } from 'rxjs';
const marks$ = from([28, 45, 33, 71, 90]);
const curved$ = marks$.pipe(
tap((m) => console.log('raw', m)),
map((m) => m + 5), // curve: add five marks
filter((m) => m >= 35), // keep passing marks only
tap((m) => console.log('passing', m)),
);
console.log('before subscribe: nothing has run');
curved$.subscribe({
next: (m) => console.log('received', m),
complete: () => console.log('done'),
});RxJS 7.8. The script was run in Node 24 twice with identical output.
Key points
- `map` transforms, `filter` drops values, `tap` runs side effects and leaves the value alone.
- They run in pipe order for each value. Nothing runs until `subscribe`.
- Use `tap` for logging and flags, not `map`.
- `filter` early to reduce work.
Common mistakes
- Expecting output without subscribing.
- Doing side effects inside `map`.
- Forgetting that `filter` drops values silently.
Follow-ups
- What is the difference between the array `map` and the RxJS `map`?
- How would you log an error in a pipe without handling it?
- Does `tap` change the value?
Tips
- Show a pipe with all three and the log output.
- Say that `pipe` returns a new observable.
- Mention subscribing.
Evaluation rubric
- Describes each operator correctly.
- Understands ordering and laziness.
- Uses `tap` for side effects.