Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Angular Templates: Null-Safe Navigation (?.)


Safely read optional values with ?. and provide defaults with ??.


What is Null-Safe Navigation (?.)?

  • ?. reads values that might be missing without throwing errors.
  • If any segment is null or undefined, the result is undefined.
  • Improves safety for deep property paths in templates.

When to use Null-Safe Navigation

  • Async or optional data where properties may be absent.
  • Use ?. (and optional chaining in code) over verbose checks.
  • Combine with ?? to provide default values.

Example

import { bootstrapApplication } from '@angular/platform-browser';
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
    <button (click)="toggle()">Toggle user</button>
    <p>Email: {{ user?.profile?.email || '(none)' }}</p>
  `
})
export class App {
  user: { profile?: { email?: string } } | undefined = undefined;
  toggle() {
    this.user = this.user ? undefined : { profile: { email: 'a@example.com' } };
  }
}

bootstrapApplication(App);
<app-root></app-root>

Run Example »

Example explained

  • Optional chaining (?.): user?.profile?.email reads email only if user and profile are defined; otherwise the whole expression is undefined (no error).
  • Fallback: || '(none)' shows a placeholder when the expression is falsy; prefer ?? when you want to treat 0 or empty strings as valid.
  • Toggle: Clicking “Toggle user” alternates user between undefined and an object to demonstrate safe access.

Nullish coalescing (??): Use a ?? b over a || b when you want to keep valid falsy values like 0 or ''.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.