Consider this Angular component using RxJS pipe to chain operators. What will be logged to the console?
import { Component } from '@angular/core'; import { of } from 'rxjs'; import { map, filter } from 'rxjs/operators'; @Component({ selector: 'app-test', template: `<p>Check console output</p>` }) export class TestComponent { constructor() { of(1, 2, 3, 4, 5).pipe( filter(x => x % 2 === 0), map(x => x * 10) ).subscribe(x => console.log(x)); } }
Remember filter removes items not matching the condition before map transforms them.
The filter operator passes only even numbers (2, 4). Then map multiplies each by 10, resulting in 20 and 40.
Which of the following code snippets correctly uses the pipe method to chain map and filter operators?
Look for correct syntax with commas and parentheses.
The pipe method takes operators as separate arguments separated by commas inside parentheses.
Given this code snippet, why does it cause a runtime error?
import { of } from 'rxjs'; import { map, filter } from 'rxjs/operators'; of(1, 2, 3).pipe( map(x => x * 2), filter(x => x > 3), map(x => x.toUpperCase()) ).subscribe(console.log);
Check the data type before calling toUpperCase.
The map operator transforms numbers, but toUpperCase is a string method. Calling it on a number causes a TypeError.
What value will be emitted by this observable chain?
import { of } from 'rxjs'; import { scan, filter } from 'rxjs/operators'; of(1, 2, 3, 4).pipe( scan((acc, val) => acc + val, 0), filter(sum => sum > 5) ).subscribe(x => console.log(x));
Think about how scan accumulates values and when filter passes them.
scan sums values cumulatively: 1, 3, 6, 10. filter passes only sums greater than 5, so 6 and 10 are emitted.
Which reason best explains why using pipe to chain RxJS operators is better than nesting subscriptions?
Think about code clarity and managing multiple async steps.
Chaining operators with pipe creates a clear, linear flow of data transformations and avoids deeply nested callbacks, improving readability and maintainability.