← Back to Angular, page 4

angular-0062 · Easy fresher

What can and cannot be used in an Angular template expression, and how do the safe navigation operator, nullish coalescing, non-null assertion and @let work?

Template expressions are a restricted subset of JavaScript. They support property access, method calls, ternaries, pipes, the safe navigation operator (?.), nullish coalescing (??), and the non-null assertion (!). They do not allow assignments with side effects like ++, new, arrow functions, or access to globals. @let declares a read-only template variable.

Full answer

An expression inside {{ }} or a binding is evaluated against the component class. Angular keeps the language small so that expressions stay fast and free of side effects.

Allowed:

  • Property reads and method calls: student().name, format(date).
  • Operators: arithmetic, comparison, &&, ||, !, ternary a ? b : c, nullish coalescing a ?? b.
  • Safe navigation user?.address?.city. If user is null or undefined the result is undefined, with no error. Signals use user()?.name.
  • Non-null assertion value!. It only silences the compiler (it does nothing at runtime), so use it when you know the value exists.
  • Pipes: {{ price | currency }}.
  • Newer additions: template literals (`Hello ${name}`, Angular 19.2), typeof (Angular 19), and in and exponentiation ** (Angular 20).

Not allowed:

  • Assignments inside interpolation, ++, --, new, bitwise operators, and chained statements with ; (event handlers allow several statements).
  • Arrow functions and function expressions. Move that logic into a method or a computed.
  • Globals such as window, Math, or console. Expose what you need as a field of the component.

@let (stable in Angular 19). @let total = price() * qty(); declares a variable that is available below it in the same template (and in nested blocks), and it updates when its inputs change. It cannot be reassigned. Use it for a value used several times, or to unwrap an Observable: @let user = user$ | async;.

Keep expressions short. A long expression is a sign to move the logic to a computed.

typescript
import { DecimalPipe } from '@angular/common';
import { Component, signal } from '@angular/core';

interface Address {
  city: string;
}

interface Student {
  name: string;
  marks: number;
  address?: Address;
}

@Component({
  selector: 'app-expression-demo',
  template: `
    @let s = student();
    @let percent = (s.marks / 150) * 100;

    <p>{{ s.name }} lives in {{ s.address?.city ?? 'unknown city' }}</p>
    <p>{{ percent | number: '1.0-1' }} percent</p>
    <!-- Backticks and dollar signs are escaped only because this template is itself a TS template string. -->
    <p>{{ \`Hello \${s.name}\` }}</p>
    <p>{{ s.marks > 90 ? 'Top' : 'Average' }}</p>
    <p>{{ 2 ** 3 }} {{ 'name' in s }} {{ typeof s.marks }}</p>
    <p>Rounded: {{ round(percent) }}</p>
  `,
  imports: [DecimalPipe],
})
export class ExpressionDemoComponent {
  readonly student = signal<Student>({ name: 'Asha', marks: 120 });

  // Math is not available in templates, so wrap what you need.
  round(value: number): number {
    return Math.round(value);
  }
}

Angular 20.3, strict template check passes. The `typeof`, `in`, and `**` operators need Angular 20 (template literals need 19.2).

Key points

  • Expressions are a safe subset of JavaScript evaluated against the component.
  • `?.` and `??` handle missing values. `!` only silences the compiler.
  • No arrow functions, `new`, globals, or `++`. Use a method or `computed`.
  • `@let` declares a read-only variable in a template.

Common mistakes

  • Using `!` to hide a possible `null` and getting a runtime error later.
  • Trying `Math.round(x)` in a template. `Math` is not available. Add a method or a pipe.
  • Writing an arrow function in a template and getting a parser error.

Follow-ups

  • Why are arrow functions not allowed in templates?
  • How is `@let` different from `*ngIf="x as y"`?
  • When would you move an expression into a `computed`?

Tips

  • Say that `!` is a compile-time tool only. This is a common trap.
  • Name the global-access rule and the fix.
  • Mention `@let` as a modern replacement for repeated expressions.

Evaluation rubric

  • Lists allowed and disallowed syntax correctly.
  • Understands ?., ??, and ! semantics.
  • Uses @let appropriately.