Bird
0
0

What will be logged to the console when this Angular component is destroyed?

medium📝 component behavior Q5 of 15
Angular - Lifecycle Hooks
What will be logged to the console when this Angular component is destroyed?
export class LoggerComponent implements OnDestroy {
  private subscription = observable$.subscribe();

  ngOnDestroy() {
    this.subscription.unsubscribe();
    console.log('Cleanup done');
  }
}
A'Cleanup done' will be logged after unsubscribing
BNothing will be logged because unsubscribe() is asynchronous
CAn error will occur because unsubscribe() is missing parentheses
D'Cleanup done' will be logged before unsubscribing
Step-by-Step Solution
Solution:
  1. Step 1: Analyze ngOnDestroy method

    The method calls unsubscribe() on the subscription, then logs a message.
  2. Step 2: Understand unsubscribe behavior

    Unsubscribing is synchronous and prevents memory leaks.
  3. Step 3: Console output

    After unsubscribing, 'Cleanup done' is logged.
  4. Final Answer:

    'Cleanup done' will be logged after unsubscribing -> Option A
  5. Quick Check:

    Unsubscribe then log [OK]
Quick Trick: Unsubscribe first, then log cleanup message [OK]
Common Mistakes:
  • Thinking unsubscribe is asynchronous
  • Forgetting parentheses on unsubscribe()
  • Logging before unsubscribing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes