DEV Community

Cover image for 🧠 Understanding `@let` in Angular Templates: A Complete Guide
vetriselvan Panneerselvam
vetriselvan Panneerselvam

Posted on

7 3 3 4 3

🧠 Understanding `@let` in Angular Templates: A Complete Guide

Angular’s template syntax is powerful—and with the introduction of the @let in Angular's syntax, it becomes even cleaner and more expressive. In this post, we’ll explore what @let is, how to use it, and some practical examples to help you get started.

🚀 What is @let?

The @let allows you to declare local variables in your Angular template that can be reused throughout the same scope or nested scopes—much like how you'd use let in JavaScript or TypeScript.

Syntax:

@let variableName = value;
Enter fullscreen mode Exit fullscreen mode

✅ Simple Examples

Let’s start with a few basic usages.

@let count = 1;
@let countryList = country$ | async;
Enter fullscreen mode Exit fullscreen mode

Here, count is a simple number, and countryList is assigned the async value of an observable.

🧪 Key Rules

  • Variables can only be used after they’ve been declared.
  • They’re accessible within the same scope or nested scopes.
  • Declaring a variable inside a block (like @if) will limit its accessibility to that block.

📦 Usage Example

Let’s see a more practical scenario involving a list of countries.

@let text = 'Hello';
<div class="country-list">
  @if (country$ | async; as countryList) {
    @for (country of countryList; track $index;
      let index = $index, even = $even, odd = $odd,
      last = $last, first = $first, count = $count) {

      <div class="country">
        <div class="label">
          <span>{{ index }} : {{ country.flag }}</span>
          <span>{{ country.name.common }}</span>
        </div>
        <hr />
      </div>
    }
    <span>text: {{ text }}</span>
  }
</div>
Enter fullscreen mode Exit fullscreen mode

You can also bind values from the component’s ts file.

In the component:

date = new Date().toDateString();
Enter fullscreen mode Exit fullscreen mode

In the template:

@let time = date;
Enter fullscreen mode Exit fullscreen mode
<div class="country-list">
  @if (country$ | async; as countryList) {
    @for (country of countryList; track $index;
      let index = $index, even = $even, odd = $odd,
      last = $last, first = $first, count = $count) {

      <div class="country">
        <div class="label">
          <span>{{ index }} : {{ country.flag }}</span>
          <span>{{ country.name.common }}</span>
        </div>
        <hr />
        <span>time: {{ time | json }}</span>
      </div>
    }
  }
</div>
Enter fullscreen mode Exit fullscreen mode

🧭 Scope Behavior: @let Inside vs. Outside

✅ Declared Outside a Scope

@let currentDate = date;
<div class="country-list">
  @if (country$ | async; as countryList) {
    @for (country of countryList; track $index;
      let index = $index, ... ) {
      <div class="country">
        ...
        <span>time: {{ currentDate | json }}</span>
      </div>
    }
  }
</div>

{{ currentDate }} <!-- ✅ This works -->
Enter fullscreen mode Exit fullscreen mode

Since @let currentDate is declared at the top level, it is available throughout the entire template.

❌ Declared Inside a Scope

@if (true) {
  @let currentDate = date;
  ...
}

{{ currentDate }} <!-- ❌ Error: Property 'currentDate' does not exist -->
Enter fullscreen mode Exit fullscreen mode

When you declare @let inside a scope (like @if), it is not accessible outside that block.

❌ Used Before Declaration

<div class="country-list">
  {{ currentDate }} <!-- ❌ Error -->
  @let currentDate = date;
  ...
</div>
Enter fullscreen mode Exit fullscreen mode

Just like in JavaScript, using a variable before it’s declared results in an error.

🧵 Conclusion

Using @let can simplify your Angular templates by removing redundant pipes and enabling scoped variable reuse. But be mindful of where you declare your variables to avoid scope-related issues.

Have you tried @let in your Angular apps yet? Let me know in the comments how you're using it or if you ran into any challenges!

📌 If you found this helpful, don’t forget to follow for more Angular tips!

✍️ Author: Vetriselvan

👨‍💻 Frontend Developer | Code Lover | Exploring Angular’s future

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server 🏁

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

Top comments (6)

Collapse
 
devops_fundamental profile image
DevOps Fundamental

Insightful and well-written as always!

Collapse
 
vetriselvan_11 profile image
vetriselvan Panneerselvam

Thanks for your feedback

Collapse
 
testfeed profile image
Testfeed

cool!

Collapse
 
nevodavid profile image
Nevo David

Pretty cool seeing Angular add stuff that cuts down the mess in templates. I'm definitely gonna mess with this soon.

Collapse
 
dotallio profile image
Dotallio

I've started using @let and it really helped clean up my templates by cutting out extra async pipes. Did you run into any edge cases where it didn't work as expected?

Collapse
 
vetriselvan_11 profile image
vetriselvan Panneerselvam

@dotallio . Thanks for your feedback.
So far, I haven't encountered any issues. I will explore further and let you know if anything is not working as expected.

Build the product, not the plumbing—JavaScript first

Build the product, not the plumbing—JavaScript first

Kinde handles the boring but critical stuff: auth, permissions, and billing—all from one Javascript SDK.

Get a free account

👋 Kindness is contagious

Explore this practical breakdown on DEV’s open platform, where developers from every background come together to push boundaries. No matter your experience, your viewpoint enriches the conversation.

Dropping a simple “thank you” or question in the comments goes a long way in supporting authors—your feedback helps ideas evolve.

At DEV, shared discovery drives progress and builds lasting bonds. If this post resonated, a quick nod of appreciation can make all the difference.

Okay