What is ngOnInit's purpose and why prefer it over the constructor?

Tests Angular lifecycle timing and input binding. Strong answer: ngOnInit runs once after inputs are initialized, but the constructor instantiates the class before bindings exist.
What's really being asked
This question probes your understanding of Angular component lifecycle timing and the boundary between JavaScript class mechanics and Angular framework behavior. Interviewers want to see that you know when input properties become available and why initialization logic belongs in a lifecycle hook rather than a class constructor.
The full answer
First, define the constructor as the standard JavaScript class constructor that runs when Angular instantiates the component class. Second, define ngOnInit as the lifecycle hook that runs exactly once after Angular has initialized all of the component inputs with their initial values. Third, explain the practical implication: inside the constructor, input properties are not yet bound, so accessing them yields undefined or default values, whereas inside ngOnInit they are guaranteed to have their first bound values. Fourth, mention that during initialization the first ngOnChanges runs before ngOnInit, which is relevant for input change inspection. Fifth, note that heavy setup logic such as service calls or state derivation should live in ngOnInit to keep the constructor lightweight and focused on dependency injection assignments.
The mistakes people make
Claiming that the constructor should never contain any logic at all; while lightweight is preferred, DI assignments happen there. Asserting that ngOnInit is a TypeScript requirement or compiler artifact rather than an Angular framework contract. Saying that inputs are available in the constructor because they look like class properties. Suggesting that ngOnInit runs multiple times during the component life. Stating that ngOnInit runs before ngOnChanges during initialization.
What usually comes next
How would you react to input changes after initialization? When would you use ngOnChanges instead of ngOnInit? What happens if you modify component state inside ngOnChanges during initialization? Can you perform DOM manipulation in ngOnInit? Why should constructors stay lean when using server-side rendering?
A concrete example
Imagine a UserProfile component with an input called userId. In the constructor, this userId is still unset because Angular has not yet bound the input from the parent template. If you call a userService fetchProfile method with this userId inside the constructor, you pass undefined. Moving the same call to ngOnInit guarantees that userId has the value passed from the parent, so the service request is valid. If you also need to detect subsequent changes to userId, you would implement ngOnChanges, but the initial safe read still belongs in ngOnInit.
Interview question
Why should initialization logic that reads @Input values be placed in ngOnInit instead of the constructor?
- a.Because ngOnInit is a TypeScript compiler requirement that ensures input type safety.
- b.Because ngOnInit executes before the constructor during component instantiation.
- c.Because input properties are guaranteed to have their initial bound values in ngOnInit but not in the constructor.Correct
- d.Because the constructor should never contain any logic whatsoever.
Why? this is the answer
Angular initializes input bindings after class instantiation, so @Input values are undefined in the constructor but available in ngOnInit. Distractor A is wrong because constructors are still the correct place for dependency injection assignments, even if heavy setup logic should be avoided.
Just read this? Test yourself on what you have been reading.
Read the original → angular.dev
- #angular
- #lifecycle
- #components
- #ngoninit
- #constructor
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on angular — each one lists the topics its interview covers.
See open roles