Bird
0
0

What will be logged to the console when this Angular code runs?

medium📝 component behavior Q13 of 15
Angular - HTTP Client
What will be logged to the console when this Angular code runs?
this.http.get('https://api.example.com/items').subscribe(data => console.log(data));
AThe data received from the API endpoint
BAn error message because subscribe is missing
CNothing, because get() does not return data
DA syntax error due to wrong method usage
Step-by-Step Solution
Solution:
  1. Step 1: Understand HttpClient.get() behavior

    The get() method returns an observable that emits the data from the API.
  2. Step 2: Effect of subscribe()

    Calling subscribe() listens to the observable and runs the callback with the data, logging it.
  3. Final Answer:

    The data received from the API endpoint -> Option A
  4. Quick Check:

    subscribe logs API data [OK]
Quick Trick: subscribe callback logs the fetched data [OK]
Common Mistakes:
MISTAKES
  • Assuming get() returns data directly without subscribe
  • Thinking subscribe is optional
  • Confusing observable with promise

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes