Angular
Toolbox
Created: 11 Jan 2026
Updated: 11 Jan 2026
@let keyword
modern Angular (v18.1+), the @let syntax is almost always used alongside the new control flow (@if, @for).
Here is a basic, clean example of a User Dashboard component that calculates a discount and handles an asynchronous name.
The Modern Syntax Example
This example demonstrates how @let simplifies template logic without needing extra <div> or <ng-container> wrappers.
@let user = user$ | async;
@let discount = product.price * 0.2;
@let finalPrice = product.price - discount;
<div class="card">
@if (user) {
<h2>Welcome back, {{ user.name }}!</h2>
<div class="pricing">
<p>Original Price: {{ product.price | currency }}</p>
<p class="promo">Your 20% Discount: -{{ discount | currency }}</p>
<hr>
<h3>Final Price: {{ finalPrice | currency }}</h3>
</div>
} @else {
<p>Please log in to see your custom pricing.</p>
}
</div>
@for (item of items; track item.id) {
@let isExpensive = item.price > 100;
<div [class.highlight]="isExpensive">
{{ item.name }} - {{ item.price }}
@if (isExpensive) { <span>(Premium Item)</span> }
</div>
}
Why this is better than the "Old Way"
- No "Falsy" Bugs: With the old
*ngIf="value$ | async as val", if the value was0orfalse, the whole block would disappear from the screen.@letsimply stores the value, so the UI stays visible even if the value is0. - Flat Templates: You don't have to nest five
<ng-container>tags just to alias five different Observables. You just list your@letdeclarations at the top. - Performance: In the
finalPriceexample above, the mathproduct.price - discountis calculated once when the data changes, rather than being recalculated every time Angular runs change detection for that specific text binding.
Key Rules for @let
- Location: You must declare
@letbefore you use it (no hoisting). - Scope: If you declare
@letinside an@ifblock, it is only available inside that block. - Read-Only: You cannot change the value of a
@letvariable via a button click or event (e.g.,<button (click)="myVar = 5">will not work). It is meant for presentation logic only.