When is effect called for the first time?
effect() is called immediately upon creation, once, to capture the initial state.
The order is as follows:
- You create
effect(() => console.log(counter())). - Angular immediately runs this code, printing the current value of
counter(). - After that it is called every time
counterchanges.
Example:
ts
const counter = signal(0);
effect(() => {
console.log('counter:', counter());
});Console output:
javascript
counter: 0 ← first call on creation
counter: 1 ← after counter.set(1)
counter: 2 ← after counter.set(2)So the first run is simply initialization, so the effect immediately "knows" the current state.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.