What is effect() in Angular?
effect() is a way to react to signal changes.
When a signal changes, the code inside effect() runs automatically.
Example:
ts
import { signal, effect } from '@angular/core';
const counter = signal(0);
effect(() => {
console.log('New value:', counter());
});If you call counter.set(5), the console will show New value: 5.
Why it is needed
effect() is used for side effects that are not directly tied to the template:
- logging,
- server requests,
- writing data to localStorage,
- updating the DOM outside Angular.
The main point:
effect()tracks on its own which signals it reads;- it re-runs the code when they change;
- it returns nothing, it simply reacts.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.