About 53,400 results
Open links in new tab
  1. Optional chaining (?.) is a feature in JavaScript that simplifies accessing properties and methods of nested objects or arrays, especially when intermediate properties might be null or undefined. This operator prevents runtime errors by short-circuiting the evaluation if any part of the chain is null or undefined, returning undefined instead of throwing an error.

    Accessing Nested Properties

    Traditionally, accessing nested properties required multiple checks to ensure each property in the chain exists:

    const user = { name: "John", address: { city: "New York", zipcode: "10001" } };

    let city;
    if (user && user.address && user.address.city) {
    city = user.address.city;
    } else {
    city = "Unknown";
    }
    console.log("Traditional Approach:", city); // Output: New York
    Copied!

    With optional chaining, this can be simplified:

    const city = user?.address?.city || "Unknown";
    console.log("Optional Chaining Approach:", city); // Output: New York
    Copied!
    Feedback
  2. Optional chaining (?.) - JavaScript | MDN

    Learn how to use the optional chaining (?.) operator to access an object's property or call a function without throwing an error if the reference is nullish. See syntax, example…
    Overview

    The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called using this operator is undefined or null, the expression short circuits and evaluates to undefined instead of throwin…

    Description

    The ?. operator is like the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined. When used with function calls, it returns undefin…

    Examples

    Basic example
    This example looks for the value of the name property for the member bar in a map when there is no such member. The result is therefore undefined.
    Dealing with optional callbacks or event h…

    Browser compatibility

    BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

  3. JavaScript Optional Chaining - GeeksforGeeks

    Jan 12, 2026 · Optional Chaining (ES2020) safely accesses properties or calls functions on null or undefined values. Safely accesses nested properties without …

  4. Optional chaining - The Modern JavaScript Tutorial

    Learn how to use the optional chaining ?. syntax to access nested object properties safely, even if an intermediate property doesn’t exist. See examples, advantages, …

  5. Optional Chaining in JavaScript – Explained with Examples

    Feb 13, 2024 · Learn how to use optional chaining (?.) to access nested properties and methods without null or undefined checks. See practical examples of optional chaining in action with user objects and …

  6. How Optional Chaining Really Works | Medium

    May 25, 2025 · Optional chaining gives you a way to safely walk through an object’s structure without crashing the program when something in the chain is missing. …

  7. How to Use Optional Chaining (?.) in JavaScript - Tutorial Reference

    Optional chaining (?.) is a modern JavaScript operator (introduced in ES2020) that lets you safely access nested properties, methods, and bracket-notation values without worrying about whether an …

  8. People also ask
    Loading
    Unable to load answer
  9. Optional Chaining (.?) in JS — Write Shorter & Safer Code!

    Jun 26, 2025 · Optional chaining is a new operator in JavaScript that lets you safely access deeply nested properties or call functions — even if some parts of the path are null or undefined.

  10. Optional Chaining in JavaScript - shefali.dev

    Jul 29, 2025 · Learn how Optional Chaining in JavaScript works and how to safely access deeply nested properties without runtime errors.

  11. JavaScript Optional Chaining Operator

    Learn how to use the optional chaining operator (?.) to access nested properties in a series of objects without checking for null or undefined values. See examples of using the operator with functions, …

  12. Optional-Chaining Operator (`?.`) in JavaScript - Ayodele Aransiola

    Jul 5, 2025 · The optional-chaining operator (?.) brings safety, brevity, and clarity to property access in JavaScript, especially when dealing with uncertain data shapes. By short-circuiting on null / undefined, …