Angular ngIf vs hidden: DOM removal vs CSS hiding

💡 **Frontend Interview Question for Angular Developer** **Q24:- What is the difference between `ngIf` and `hidden` in Angular?** --- ### 🔹 `ngIf` → **Completely removes or adds** the element from the DOM based on the condition. → Better for performance since Angular doesn’t render the element when `false`. ```html <div *ngIf="isVisible">Hello from ngIf 👋</div> ``` If `isVisible` is `false`, the element won’t exist in the DOM at all. --- ### 🔹 `hidden` → Just **hides the element visually** using CSS (`display: none`). → The element **still exists in the DOM**, only not visible. ```html <div [hidden]="!isVisible">Hello from hidden 👀</div> ``` If `isVisible` is `false`, it stays in DOM but invisible. --- ✅ **Key Difference:** `ngIf` → Adds/removes element from DOM. `hidden` → Hides/shows element via CSS. --- #Angular #FrontendDevelopment #AngularInterview #WebDevelopment #TypeScript #AngularTips #CodingCommunity

To view or add a comment, sign in

Explore content categories